表单身份验证正在将我注销

发布于 2024-12-09 19:57:36 字数 1687 浏览 0 评论 0原文

我正在按应用程序使用表单身份验证,当表单超时时,应用程序会将用户注销到登录页面。

但是当我再次输入我的凭据时,应用程序不允许用户进入。当我在另一个浏览器上尝试它时,它工作正常。我认为兑现或 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文