Monday, June 29, 2020

ASP.NET OWIN UseCookieAuthentication Logs User Out After Sign In

I have a need to do manual cookie authentication. As I use OWIN with UseCookieAuthentication middleware, it is not that hard except when I had no idea what is actually required.

Problem

I know I had to create a ClaimsIdentity and I will need AuthenticationProperties object. Supplying both of them successfully created the cookie, but when I went to a different page, the authentication failed and the application kicked me out to the login page. The following is my initial problematic code in the authentication handler:
Dim claims As New List(Of Claim)
claims.Add(New Claim(ClaimTypes.NameIdentifier, user.Username))
Dim claimsIdentity As New ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationType)
context.GetOwinContext().Authentication.SignIn(New AuthenticationProperties With {
    .ExpiresUtc = expiration
}, claimsIdentity)

Solution

For my basic requirement, apparently there are required claims. In my case, I'm missing the Name claim. Adding the following claim solves my issue:
claims.Add(New Claim(ClaimTypes.Name, user.Name))
As of now, I'm still not sure why it is necessary and haven't had time to look it up. But a quick read on ClaimsIdentity reveal that NameClaimType is necessary.


My final code thus becomes:

Dim claims As New List(Of Claim)
claims.Add(New Claim(ClaimTypes.NameIdentifier, user.Username))
claims.Add(New Claim(ClaimTypes.Name, user.Name))
Dim claimsIdentity As New ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationType)
context.GetOwinContext().Authentication.SignIn(New AuthenticationProperties With {
.ExpiresUtc = expiration
}, claimsIdentity)

No comments:

Post a Comment