Search code examples
owinclaims-based-identityclaimsopenid-connect

Can't set ClaimIdentity


I have a set of claims. I'm using it to create a ClaimsIdentity. I also use OWIN to signin the identity. In addition, I'm adding it to the ClaimsPrincipal.Current.Identities. Here is my code...

[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> LogonCallBack()
{
    var token = Request.Params["id_token"];

    var validatedToken = TokenService.ValidateIdToken(token);
    var identity = new ClaimsIdentity(validatedToken.Claims);
    HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties { IsPersistent = false }, identity);
    ClaimsPrincipal.Current.AddIdentity(identity);

    return RedirectToAction("Display");

    //return RedirectToAction("Index", "Error", new { area = "Token Validate Failed." });
}

When debugging, I see that the set of claims that i am retrieving are coming across fine. And I can create the ClaimsIdentity. However, after this, when I am redirected to the Display page, the User.Identity.IsAuthenticated is still false. ClaimsPrincipal.Current does not have the added identity in its list.

How am I able to get the user to be authenticated?


Solution

  • I added the cookietype string to the identity declaration:

    var identity = new ClaimsIdentity(validatedToken.Claims, DefaultAuthenticationTypes.ApplicationCookie);
    

    And I also added the same string to the middleware pipeline as follows:

    app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ApplicationCookie);
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/OIDC/Authenticate")
            });