【问题标题】:Can't set ClaimIdentity无法设置 ClaimsIdentity
【发布时间】:2018-08-02 22:08:03
【问题描述】:

我有一组声明。我正在使用它来创建 ClaimsIdentity。我还使用 OWIN 登录身份。此外,我将它添加到 ClaimsPrincipal.Current.Identities。这是我的代码...

[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." });
}

在调试时,我发现我正在检索的一组声明运行良好。我可以创建 ClaimsIdentity。但是,在此之后,当我被重定向到显示页面时,User.Identity.IsAuthenticated 仍然是错误的。 ClaimsPrincipal.Current 在其列表中没有添加的标识。

我怎样才能让用户通过身份验证?

【问题讨论】:

    标签: owin claims-based-identity claims openid-connect


    【解决方案1】:

    您应该使用SecurityHelper 向当前用户添加身份。

    var owinContext = HttpContext.GetOwinContext();
    var securityHelper = new Microsoft.Owin.Security.Infrastructure.SecurityHelper(owinContext);
    securityHelper.AddUserIdentity(identity); 
    

    【讨论】:

      【解决方案2】:

      我在身份声明中添加了 cookietype 字符串:

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

      并且我还在中间件管道中添加了相同的字符串,如下所示:

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

      【讨论】:

        猜你喜欢
        • 2021-11-22
        • 2019-02-09
        • 2020-09-03
        • 2016-01-28
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 2015-12-26
        • 2018-12-21
        相关资源
        最近更新 更多