带有回发的C#asp.net小部件默认登录,即使web.config不包括它.解决方法
本文关键字:web 即使 config 不包括 方法 解决 登录 默认 C#asp 小部 net | 更新日期: 2023-09-27 18:21:12
我有一个用c#、asp.net编写的输入小部件"subscribeContact.aspx"。它被排除在"web.config"中的条目的授权之外。它默认显示登录页面。如果屏幕被刷新(例如F5),则无需登录即可显示小部件。
例程"WebinarDisplay.aspx"也包含回发函数,但不显示相同的行为。
web.config条目:
<location path="~/subscribeContact.aspx">
<system.web>
<authorization>
<allow users="?"/>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="~/WebinarDisplay.aspx">
<system.web>
<authorization>
<allow users="?"/>
<allow users="*"/>
</authorization>
</system.web>
</location>
页面的来源是:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="subscribeContact.aspx.cs" Inherits="SchedulerServices.subscribeContact" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="inputDiv" runat="server">
<asp:Label ID="allFieldsRequired" runat="server" Text="All Fields are Required." Visible="true" Font-Size="Small"></asp:Label>
<asp:Label ID="selectedFieldsRequired" runat="server" Text="Fields Marked in red are Required." Visible="false" Font-Size="Small"></asp:Label>
<br />
<br />
<asp:Label ID="firstNameLabel" runat="server" Text="First Name" Font-Size="Small"></asp:Label>
<br />
<asp:TextBox ID="firstNameBox" runat="server" Font-Size="Small"></asp:TextBox>
<br />
<asp:Label ID="lastNameLabel" runat="server" Text="Last Name" Font-Size="Small"></asp:Label>
<br />
<asp:TextBox ID="lastNameBox" runat="server" Font-Size="Small"></asp:TextBox>
<br />
<asp:Label ID="emailLabel" runat="server" Text="Email Address" Font-Size="Small"></asp:Label>
<br />
<asp:TextBox ID="emailAddressBox" runat="server" Font-Size="Small" TextMode="Email"></asp:TextBox>
<br />
<asp:Label ID="phoneNumberLabel" runat="server" Text="Phone Number" Font-Size="Small"></asp:Label>
<br />
<asp:TextBox ID="phoneNumberBox" runat="server" Font-Size="Small" TextMode="Phone"></asp:TextBox>
<br />
<asp:Label ID="stateLabel" runat="server" Text="State" Font-Size="Small"></asp:Label>
<br />
<asp:DropDownList ID="stateDDL" runat="server" Font-Size="Small" >
<asp:ListItem Value="" Text="Select a state" Selected="True"></asp:ListItem>
<asp:ListItem Value="AL" Text="Alabama" ></asp:ListItem>
<asp:ListItem Value="AK" Text="Alaska" ></asp:ListItem>
<asp:ListItem Value="AZ" Text="Arizona" ></asp:ListItem>
<asp:ListItem Value="AR" Text="Arkansas" ></asp:ListItem>
<asp:ListItem Value="CA" Text="California" ></asp:ListItem>
<asp:ListItem Value="CO" Text="Colorado" ></asp:ListItem>
<asp:ListItem Value="CT" Text="Connecticut" ></asp:ListItem>
<asp:ListItem Value="DE" Text="Delaware" ></asp:ListItem>
<asp:ListItem Value="DC" Text="District of Columbia" ></asp:ListItem>
<asp:ListItem Value="FL" Text="Florida" ></asp:ListItem>
<asp:ListItem Value="GA" Text="Georgia" ></asp:ListItem>
<asp:ListItem Value="HI" Text="Hawaii" ></asp:ListItem>
<asp:ListItem Value="ID" Text="Idaho" ></asp:ListItem>
<asp:ListItem Value="IL" Text="Illinois" ></asp:ListItem>
<asp:ListItem Value="IN" Text="Indiana" ></asp:ListItem>
<asp:ListItem Value="IA" Text="Iowa" ></asp:ListItem>
<asp:ListItem Value="KS" Text="Kansas" ></asp:ListItem>
<asp:ListItem Value="KY" Text="Kentucky" ></asp:ListItem>
<asp:ListItem Value="LA" Text="Louisiana" ></asp:ListItem>
<asp:ListItem Value="ME" Text="Maine" ></asp:ListItem>
<asp:ListItem Value="MD" Text="Maryland" ></asp:ListItem>
<asp:ListItem Value="MA" Text="Massachusetts" ></asp:ListItem>
<asp:ListItem Value="MI" Text="Michigan" ></asp:ListItem>
<asp:ListItem Value="MN" Text="Minnesota" ></asp:ListItem>
<asp:ListItem Value="MS" Text="Mississippi" ></asp:ListItem>
<asp:ListItem Value="MO" Text="Missouri" ></asp:ListItem>
<asp:ListItem Value="MT" Text="Montana" ></asp:ListItem>
<asp:ListItem Value="NE" Text="Nebraska" ></asp:ListItem>
<asp:ListItem Value="NV" Text="Nevada" ></asp:ListItem>
<asp:ListItem Value="NH" Text="New Hampshire" ></asp:ListItem>
<asp:ListItem Value="NJ" Text="New Jersey" ></asp:ListItem>
<asp:ListItem Value="NM" Text="New Mexico" ></asp:ListItem>
<asp:ListItem Value="NY" Text="New York" ></asp:ListItem>
<asp:ListItem Value="NC" Text="North Carolina" ></asp:ListItem>
<asp:ListItem Value="ND" Text="North Dakota" ></asp:ListItem>
<asp:ListItem Value="OH" Text="Ohio" ></asp:ListItem>
<asp:ListItem Value="OK" Text="Oklahoma" ></asp:ListItem>
<asp:ListItem Value="OR" Text="Oregon" ></asp:ListItem>
<asp:ListItem Value="PA" Text="Pennsylvania" ></asp:ListItem>
<asp:ListItem Value="RI" Text="Rhode Island" ></asp:ListItem>
<asp:ListItem Value="SC" Text="South Carolina" ></asp:ListItem>
<asp:ListItem Value="SD" Text="South Dakota" ></asp:ListItem>
<asp:ListItem Value="TN" Text="Tennessee" ></asp:ListItem>
<asp:ListItem Value="TX" Text="Texas" ></asp:ListItem>
<asp:ListItem Value="UT" Text="Utah" ></asp:ListItem>
<asp:ListItem Value="VT" Text="Vermont" ></asp:ListItem>
<asp:ListItem Value="VA" Text="Virginia" ></asp:ListItem>
<asp:ListItem Value="WA" Text="Washington" ></asp:ListItem>
<asp:ListItem Value="WV" Text="West Virginia" ></asp:ListItem>
<asp:ListItem Value="WI" Text="Wisconsin" ></asp:ListItem>
<asp:ListItem Value="WY" Text="Wyoming" ></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="timeZoneLabel" runat="server" Text="Time Zone" Font-Size="Small"></asp:Label>
<br />
<asp:DropDownList ID="timezoneDDL" runat="server" Font-Size="Small">
<asp:ListItem Value="" Text="Select a state" Selected="True"></asp:ListItem>
<asp:ListItem Value="HI" Text="Hawaii" ></asp:ListItem>
<asp:ListItem Value="AK" Text="Alaska" ></asp:ListItem>
<asp:ListItem Value="PST" Text="Pacific" ></asp:ListItem>
<asp:ListItem Value="Arizona" Text="Arizona" ></asp:ListItem>
<asp:ListItem Value="MST" Text="Mountian" ></asp:ListItem>
<asp:ListItem Value="CST" Text="Central" ></asp:ListItem>
<asp:ListItem Value="EST" Text="Eastern" ></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="resumeLabel" runat="server" Text="Resume" Font-Size="Small"></asp:Label>
<asp:TextBox ID="resumeBox" runat="server" TextMode="MultiLine" Columns="50" Rows="15"></asp:TextBox>
<br />
<br />
<asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="submitButton_onClick"> </asp:Button>
</asp:Panel>
<asp:Panel ID="thankYouDiv" runat="server">
<h1>Thank You</h1>
</asp:Panel>
</form>
</body>
</html>
背后的代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace SchedulerServices
{
public partial class subscribeContact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submitButton_onClick(object sender, EventArgs e)
{
// test that fields are filled in
string xrefID = Request.QueryString["XR"];
bool sendBack = false;
firstNameLabel.ForeColor = System.Drawing.Color.Black;
if (string.IsNullOrEmpty(firstNameBox.Text.ToString()))
{
firstNameLabel.ForeColor = System.Drawing.Color.Red;
firstNameBox.Focus();
sendBack = true;
}
lastNameLabel.ForeColor = System.Drawing.Color.Black;
if (string.IsNullOrEmpty(lastNameBox.Text.ToString()))
{
lastNameLabel.ForeColor = System.Drawing.Color.Red;
lastNameBox.Focus();
sendBack = true;
}
emailLabel.ForeColor = System.Drawing.Color.Black;
if (string.IsNullOrEmpty(emailAddressBox.Text.ToString()))
{
emailLabel.ForeColor = System.Drawing.Color.Red;
emailAddressBox.Focus();
sendBack = true;
}
phoneNumberLabel.ForeColor = System.Drawing.Color.Black;
if (string.IsNullOrEmpty(phoneNumberBox.Text.ToString()))
{
phoneNumberLabel.ForeColor = System.Drawing.Color.Red;
phoneNumberBox.Focus();
sendBack = true;
}
stateLabel.ForeColor = System.Drawing.Color.Black;
if (stateDDL.SelectedValue == "")
{
stateLabel.ForeColor = System.Drawing.Color.Red;
stateDDL.Focus();
sendBack = true;
}
timeZoneLabel.ForeColor = System.Drawing.Color.Black;
if (timezoneDDL.SelectedValue == "")
{
timeZoneLabel.ForeColor = System.Drawing.Color.Red;
timezoneDDL.Focus();
sendBack = true;
}
resumeLabel.ForeColor = System.Drawing.Color.Black;
if (string.IsNullOrEmpty(resumeBox.Text.ToString()))
{
resumeLabel.ForeColor = System.Drawing.Color.Red;
resumeBox.Focus();
sendBack = true;
}
if (sendBack)
{
allFieldsRequired.Visible = false;
selectedFieldsRequired.Visible = true;
return;
}
// get the account ID and User ID
// put the information in a record layout
// write record
inputDiv.Visible = false;
thankYouDiv.Visible = true;
}
}
}
我被这件事难住了。其他独立页面工作起来没有问题,包括使用非排除区域中存在的代码。一旦页面加载,它就会正常工作。
问题是,无论web.config排除在外,回发都将执行login.aspx。
答案在于修改login.aspx.cs文件以在Page_Load部分包含以下代码:
protected void Page_Load(object sender, EventArgs e)
{
string returnURL = Request.QueryString["ReturnUrl"];
if (!string.IsNullOrEmpty(returnURL))
{
// Test for the exclusion
if (returnURL.Length > 7)
{
if (returnURL.Substring(1, 7) == "Webinar") // or whatever you want to continue on to processing
{
Response.Redirect(returnURL);
}
if (returnURL.Substring(1, 9) == "subscribe") // or whatever you want to continue on to processing
{
Response.Redirect(returnURL);
}
}
if (System.Web.HttpContext.Current.User != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
Response.Redirect(returnURL); // this exists to allow users to re-log in if the session has not timed out
}
}
RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]); // use this if you want the person directed to a registration page
ResendEmailHyperLink.NavigateUrl = "Resend.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]); // use this if you want the person to have the registration email resent to them
ResetHyperLink.NavigateUrl = "ResetPassword.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]); // use this to allow the user to reset their password
}
这现在起作用了。因此,解决方法是对Login.asp.cs(或您的登录页面)进行编码,以专门重定向到ReturnURL中的值。我在跟踪流程时重新发现了我的解决方法,并看到了我4年多前创建的工作。
我希望这能帮助其他人了解如何使用直接的C#、ASP.NET代码编写小部件,而不必使用JSON、JQUERY或MVC 进行更奇特的解决方案