ASP.NET中的表单认证——>无法在远程服务器上的Webkit浏览器上重定向到受保护的页面
本文关键字:浏览器 重定向 服务器 Webkit 受保护 表单 NET 认证 ASP | 更新日期: 2023-09-27 17:49:56
我已经通过使用一些自定义代码来设置cookie来实现表单身份验证。我能够在我的开发服务器上的所有浏览器上正确登录后将用户重定向到适当的ReturnUrl。我用的是ASP。. NET 3.5 Web Forms.
在我用于部署的远程服务器上,登录页面只是重新加载,而不是重定向到ReturnUrl。奇怪的是,这只发生在Webkit浏览器上。壁虎浏览器工作良好。我到处寻找解决方案,但没有找到任何类似的。
网络。配置(我想重定向到管理员。aspx form Login.aspx):
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="none"/>
<add key="loginUrl" value="Login.aspx" />
</appSettings>
<system.web>
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="Login.aspx" defaultUrl="Admin.aspx" protection="All" timeout="2880" cookieless="UseCookies" />
</authentication>
...
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
<location path ="Default.aspx">
<system.web>
<authorization>
<allow users ="*"/>
</authorization>
</system.web>
</location>
<location path="Resources">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="Admin.aspx">
<system.web>
<authorization>
<deny users ="?"/>
</authorization>
</system.web>
</location>
Login.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Submit_Click(object sender, EventArgs e)
{
DatabaseCommands c = new DatabaseCommands();
if (c.CheckCredentials(txtUsername.Text, txtPassword.Text))
{
FormsAuthenticationTicket ticket;
string cookieString;
HttpCookie cookie;
ticket = new FormsAuthenticationTicket(1, txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(1), true, "This is one kickass ticket, yo");
cookieString = FormsAuthentication.Encrypt(ticket);
cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieString);
cookie.Expires = ticket.Expiration;
cookie.Path = FormsAuthentication.FormsCookiePath;
Response.Cookies.Add(cookie);
string strRedirect = Request["ReturnUrl"];
if (strRedirect == null)
strRedirect = "Admin.aspx";
Response.Redirect(strRedirect, true);
}
else
{
lblError.InnerText = "Invalid Credentials";
lblError.Visible = true;
}
}
. aspx:
<form id="form1" runat="server">
<div style="text-align:center">
<h1>Login</h1>
<table style="margin-right:auto;margin-left:auto;" id="loginForm">
<tr><td><label id="username">User:</label></td><td><asp:TextBox ID="txtUsername" runat="server" Width="200px"></asp:TextBox></td></tr>
<tr><td><label id="password">Password:</label></td><td><asp:TextBox runat="server" TextMode="Password" ID="txtPassword" Width="200px"></asp:TextBox></td></tr>
<tr><td><label id="lblError" runat="server" visible="false"></label></td><td><asp:Button ID="submit" Text="Submit" OnClick="Submit_Click" runat="server" align="right" PostBackUrl="/Admin.aspx" OnClientClick="Submit_Click" /></td></tr>
</table>
</div>
</form>
答案是注释掉
行 cookie.Path = ...
cookie.Expires = ...
很高兴有帮助。