asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net – 如何在不使用FormsAuthentication.RedirectFromLoginPage时将Request.IsAuthenticated设置为true?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用表单身份验证,并向服务器发送Aajx请求进行身份验证。基于json结果,客户端决定去哪里和做什么。这是我不使用FormsAuthentication.RedirectFromLoginPage不干扰ajax / json响应的原因。

在这种情况下,即使在使用Membership.ValidateUser验证用户后,@R_450_10613@est.IsAuthenticated也返回false。然后我设置cookie使用

FormsAuthentication.SetAuthCookie(username,falsE);

然第二个参数,持久cookie,是false,cookie仍然有效跨浏览器会话。

任何想法如何使@R_450_10613@est.IsAuthenticated工作,而不使用FormsAuthentication.RedirectFromLoginPage?

解决方法

您需要更新请求的当前安全主体。当您调用Response。重定向(…)一个新的请求完成,安全主体被重新初始化,并且@R_450_10613@est.IsAuthenticated在你的case返回true。 FormsAuthentication.RedirectFromLoginPage内部调用响应。重定向(…)。您可以手动更新当前请求的安全主体,如下所示:
public void RenewCurrentUser()
{
    System.Web.httpCookie authCookie =
        System.Web.httpContext.Current.@R_450_10613@est.Cookies[FormsAuthentication.FormsCookiename];
    if (authCookie != null)
    {
        FormsAuthenticationTicket authTicket = null;
        authTicket = FormsAuthentication.Decrypt(authCookie.value);

        if (authTicket != null && !authTicket.Expired)
        {
            FormsAuthenticationTicket newAuthTicket = authTicket;

            if (FormsAuthentication.SlidingExpiration)
            {
                newAuthTicket = FormsAuthentication.RenewTicketIfOld(authTicket);
            }
            String userData = newAuthTicket.UserData;
            String[] roles = userData.Split(',');

            System.Web.httpContext.Current.User =
                new System.Security.Principal.GenericPrincipal(new FormsIdentity(newAuthTicket),roles);
        }
    }
}

大佬总结

以上是大佬教程为你收集整理的asp.net – 如何在不使用FormsAuthentication.RedirectFromLoginPage时将Request.IsAuthenticated设置为true?全部内容,希望文章能够帮你解决asp.net – 如何在不使用FormsAuthentication.RedirectFromLoginPage时将Request.IsAuthenticated设置为true?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。