表单身份验证正在将我注销
我正在按应用程序使用表单身份验证,当表单超时时,应用程序会将用户注销到登录页面。
但是当我再次输入我的凭据时,应用程序不允许用户进入。当我在另一个浏览器上尝试它时,它工作正常。我认为兑现或 cookie 可能存在问题,所以我从浏览器中清除了缓存和 cookie 。但这仍然不起作用。
我担心我的代码中可能会出现一些问题。
可以请一些人检查一下吗?
非常感谢
protected void ManageRoles(string userid, string role)
{
FormsAuthentication.Initialize();
FormsAuthenticationTicket Authticket = new FormsAuthenticationTicket(1, userid, DateTime.Now, DateTime.Now.AddMinutes(180), true, role, FormsAuthentication.FormsCookieName);
string hash = FormsAuthentication.Encrypt(Authticket);
HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
if (Authticket.IsPersistent)
{
Authcookie.Expires = Authticket.Expiration;
Response.Cookies.Add(Authcookie);
}
}
Global.asax
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket Authticket = id.Ticket;
string userData = Authticket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = new GenericPrincipal(id, roles);
}
}
}
}
I am using form authentication for by application and when the form times-out the application logs the user out to login page.
But when I enter my credentials again, the application is not letting the user in. When I try it on another browser it works fine.I thought there might be problem with the cashing or cookies, so I cleared both cache and cookies from the browser. But this is still not working.
I am afraid that some thing might me wrong in my code.
Can some please check?
Many Thanks
protected void ManageRoles(string userid, string role)
{
FormsAuthentication.Initialize();
FormsAuthenticationTicket Authticket = new FormsAuthenticationTicket(1, userid, DateTime.Now, DateTime.Now.AddMinutes(180), true, role, FormsAuthentication.FormsCookieName);
string hash = FormsAuthentication.Encrypt(Authticket);
HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
if (Authticket.IsPersistent)
{
Authcookie.Expires = Authticket.Expiration;
Response.Cookies.Add(Authcookie);
}
}
Global.asax
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket Authticket = id.Ticket;
string userData = Authticket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = new GenericPrincipal(id, roles);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论