将身份用户对象传递给AuthorizationFilterAttribute

时间:2019-07-17 00:33:41

标签: asp.net-identity owin

如何将完整的身份用户对象从CustomOAuthBearerAuthenticationProvider传递给AuthorizationFilterAttribute。

public class CustomOAuthBearerAuthenticationProvider : OAuthBearerAuthenticationProvider 
{       
    public override Task ValidateIdentity(OAuthValidateIdentityContext context)
    {
          var appUserManager = 
          context.OwinContext.GetUserManager<ApplicationUserManager>();
          var identity = context.Ticket.Identity;
          var user = appUserManager.FindByIdAsync(identity.GetUserId()).Result;
          context.Ticket.Identity.AddClaim(new Claim("PSK", user.PSK));
          return base.ValidateIdentity(context);
    }
}

public class TwoFactorAuthorizeAttribute : AuthorizationFilterAttribute
{
   public override Task OnAuthorizationAsync(HttpActionContext 
   actionContext, System.Threading.CancellationToken cancellationToken)
   {
       var principal = actionContext.RequestContext.Principal as 
       ClaimsPrincipal;
       var psk= principal?.FindFirst("psk").Value; //I want to read full object rather than individual property
   }
}

0 个答案:

没有答案