所以我有一个登录页面,我设置了自己的cookie和FormsAuthenticationTicket。但是,当我最终选择在登录后将用户重定向到新主页时,它会拒绝。它只是无缘无故地重定向回到登录页面。我不明白为什么。
我的web.config删除了部分机器密钥:
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" defaultUrl="~/Default.aspx" cookieless="UseCookies" name=".ASPXFORMSAUTH" timeout="50" />
</authentication>
<authorization>
<allow users="*" />
</authorization>
<machineKey decryption="AES" validation="SHA1" ........ />
输入用户名/密码并将其验证为真后,我的登录点击事件:
if (Authenticated)
{
//Create Form Authentication ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(30), false, userName, FormsAuthentication.FormsCookiePath);
string encryptedCookie = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie);
Response.Cookies.Add(cookie);
Response.Redirect("MainPage.aspx", true);
}
MasterPage检查以确保只能访问某些页面:
else if (Context.User.Identity.IsAuthenticated)
{
if (Session["uid"] == null)
{
userclass u = new userclass();
int uid = -1;
uid = (int)u.Getuseridbyusername(Context.User.Identity.Name);
if (uid != -1)
{
Session["uid"] = uid;
}
}
} else if (!Context.User.Identity.IsAuthenticated)
{
// First check if user is was redirected to ChangePassword
if (!Request.Path.Contains("ForgotPass.aspx") && !Request.Path.Contains("ChangePass.aspx") && !Request.Path.Contains("CreateAccount.aspx") && !Request.Path.Contains("Error.aspx") && !Request.Path.Contains("Logout"))
{
if (!Request.Path.Contains("Login"))
FormsAuthentication.RedirectToLoginPage();
}
}
注释掉RedirectToLoginPage()无效。尝试使用RedirectFromLoginPage无效。试图使用&lt; allow users =&#34;?&#34; /&GT;没有效果。试图使用&lt; deny users =&#34;?&#34; /&GT;结合起来没有效果。
编辑:根据浏览器流量设置Cookie。但是没有重定向。显然,要么在设置cookie后无法重定向,要么ASP.NET不知道如何阅读说明。
答案 0 :(得分:1)
解决。显然,我确实有一个Redirect,它将用户带回登录页面,即使设置了cookie并且Context.User.Identity.IsAuthenticated返回true,因为正在设置会话变量“uid”。
答案 1 :(得分:1)
在配置文件
中使用它<authorization>
<deny users="?" />
<allow users="*" />
</authorization>