cdub
cdub

Reputation: 25711

FormsAuthentication.SetAuthCookie(user.UserName, true); cookie name

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.

Upvotes: 0

Views: 2463

Answers (2)

Win
Win

Reputation: 62260

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.

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

Global.asax.cs

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

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

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

Usage

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

Browser Plugin

I use EditThisCookie Chrome Plugin.

enter image description here

Upvotes: 1

NikolaiDante
NikolaiDante

Reputation: 18639

What is this cookie called when it gets set?

The default name for the cookie is .ASPXAUTH. It's configurable through the web.config to have any name.

<authentication mode="Forms">
  <forms name=".SomeName" loginUrl="Login.aspx" />
</authentication>

So check there for anything non-standard.

Can I see it in my cookie folder for my browser?

You should be able to see it using any browser tools.

Upvotes: 1

Related Questions