[{"data":1,"prerenderedAt":102},["ShallowReactive",2],{"/2025/12/07-asp.net-owin-usecookieauthentication-logs-user-out-after-sign-in":3},{"id":4,"title":5,"body":6,"date":93,"description":49,"extension":94,"meta":95,"navigation":96,"path":97,"robots":98,"seo":99,"stem":100,"__hash__":101},"posts/2025/12/07 ASP.NET-OWIN-UseCookieAuthentication-logs-user-out-after-sign-in.md","07 ASPNET OWIN UseCookieAuthentication Logs User Out After Sign In",{"type":7,"value":8,"toc":90},"minimark",[9,18,25,28,31,33,36,50,52,55,63,66,79,82],[10,11,13],"post-title",{":date":12},"date",[14,15,17],"h1",{"id":16},"aspnet-owin-usecookieauthentication-logs-user-out-after-sign-in","ASP.NET OWIN UseCookieAuthentication Logs User Out After Sign In",[19,20,21],"notes",{},[22,23,24],"p",{},"This is a repost from my old blog. First posted in 6/29/2020.",[26,27],"br",{},[22,29,30],{},"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.",[26,32],{},[22,34,35],{},"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:",[37,38,39],"code-block",{},[40,41,46],"pre",{"className":42,"code":44,"language":45},[43],"language-text","Dim claims As New List(Of Claim)\nclaims.Add(New Claim(ClaimTypes.NameIdentifier, user.Username))\nDim claimsIdentity As New ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationType)\ncontext.GetOwinContext().Authentication.SignIn(New AuthenticationProperties With {\n    .ExpiresUtc = expiration\n}, claimsIdentity)\n","text",[47,48,44],"code",{"__ignoreMap":49},"",[26,51],{},[22,53,54],{},"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:",[37,56,57],{},[40,58,61],{"className":59,"code":60,"language":45},[43],"claims.Add(New Claim(ClaimTypes.Name, user.Name))\n",[47,62,60],{"__ignoreMap":49},[22,64,65],{},"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.",[22,67,68],{},[69,70,73],"span",{"className":71},[72],"text-blue-600",[74,75,76],"a",{"href":76,"rel":77},"https://docs.microsoft.com/en-us/dotnet/api/system.security.claims.claimsidentity?view=netframework-4.8",[78],"nofollow",[22,80,81],{},"My final code thus becomes:",[37,83,84],{},[40,85,88],{"className":86,"code":87,"language":45},[43],"Dim claims As New List(Of Claim)\nclaims.Add(New Claim(ClaimTypes.NameIdentifier, user.Username))\nclaims.Add(New Claim(ClaimTypes.Name, user.Name))\nDim claimsIdentity As New ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationType)\ncontext.GetOwinContext().Authentication.SignIn(New AuthenticationProperties With {\n    .ExpiresUtc = expiration\n}, claimsIdentity)\n",[47,89,87],{"__ignoreMap":49},{"title":49,"searchDepth":91,"depth":91,"links":92},2,[],"2025-12-07T00:00:00.000Z","md",{},true,"/2025/12/07-asp.net-owin-usecookieauthentication-logs-user-out-after-sign-in",null,{"title":5,"description":49},"2025/12/07 ASP.NET-OWIN-UseCookieAuthentication-logs-user-out-after-sign-in","1R56eOp4xV6Ou68n6o75PQGLEBjeJDP2t6OlutHvo2A",1785167452257]