FormsAuthentication.SetAuthCookie(user.UserName,真); cookie名称 [英] FormsAuthentication.SetAuthCookie(user.UserName, true); cookie name

查看:1059
本文介绍了FormsAuthentication.SetAuthCookie(user.UserName,真); cookie名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么饼干,当它被设置叫什么名字?我可以看到它在我的cookie文件夹我的浏览器?

What is this cookie called when it gets set? Can I see it in my cookie folder for my browser?

我觉得它不会设置,所以我的登录验证失败。

I think it doesn't get set so my login verification fails.

推荐答案

通常情况下,如果创建身份验证cookie你自己,你需要创建的主要对象,并将其保存在当前线程的AuthenticateRequest 事件为每个请求。

Normally, if you create Authentication Cookie yourself, you will need to create Principal object and save it inside Current Thread in AuthenticateRequest event for every request.

另外,当您检查 User.Identity.IsAuthenticated 后,它会返回false。

Otherwise, when you check User.Identity.IsAuthenticated, it will return false.

public class Global : HttpApplication
{
    private void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        HttpCookie decryptedCookie =
            Context.Request.Cookies[FormsAuthentication.FormsCookieName];

        FormsAuthenticationTicket ticket =
            FormsAuthentication.Decrypt(decryptedCookie.Value);

        var identity = new GenericIdentity(ticket.Name);
        var principal = new GenericPrincipal(identity, null);

        HttpContext.Current.User = principal;
        Thread.CurrentPrincipal = HttpContext.Current.User;
    }
}

的web.config

请确保您有认证标记在web.config中。例如,

web.config

Make sure you have authentication tag in web.config. For example,

<authentication mode="Forms">
   <forms loginUrl="~/Account/Login" timeout="2880"/>
</authentication>

用法

public ActionResult Index()
{
    var username = User.Identity.Name;
    return View();
}

浏览器插件

我使用 EditThisCookie Chrome的插件。

Browser Plugin

I use EditThisCookie Chrome Plugin.

在这里输入的形象描述

这篇关于FormsAuthentication.SetAuthCookie(user.UserName,真); cookie名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆