2016-07-14 7 views
0

SecurityTokenValidatedイベントでアクセストークンを取得しようとしていますが、アクセストークンを取得した後、クレームに保存します。問題は、私がその主張にアクセスしようとすると、もう存在しないということです。SecurityTokenValidated Identityが失われる

public static async Task<Task> SecurityTokenValidated(SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context) 
    { 
     string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"]; 
     string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; 
     string clientSecret = ConfigurationManager.AppSettings["ida:ClientSecret"]; 
     string source = ConfigurationManager.AppSettings["ExchangeOnlineId"]; 

     var authContext = new AuthenticationContext(aadInstance, false); 
     var credentials = new ClientCredential(clientId, clientSecret); 
     var appRedirectUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase + "/"; 
     var authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(context.ProtocolMessage.Code, new Uri(appRedirectUrl), credentials, source); 
     var accessToken = authResult.AccessToken; 
     var applicationUserIdentity = new ClaimsIdentity(context.OwinContext.Authentication.User.Identity); 
     applicationUserIdentity.AddClaim(new Claim("AccessToken", accessToken)); 
     context.OwinContext.Authentication.User.AddIdentity(applicationUserIdentity); 
     return Task.FromResult(0); 
    } 

と私はこのようにそれをアクセスしよう:

SecurityTokenValidatedコードはこれです

var accessToken =((ClaimsPrincipal)HttpContext.Current.User).FindFirst("AccessToken").Value; 

更新: それは我々がHttpContext.Current上書きされますいくつかのコードを持って判明します。ユーザー。私はこのようなコードを次のように変更しました:

var claims = ((ClaimsPrincipal)HttpContext.Current.User).Claims; 
      var newPrincipal = new GenericPrincipal(new GenericIdentity(domainUserName), null); 
      ((ClaimsIdentity)newPrincipal.Identity).AddClaims(claims); 
      HttpContext.Current.User = newPrincipal; 

運がありません。私は、申し立てをうまくいくことができますが、私は新しいプリンシパルでそれらを見ることができません...

答えて

-1

私はそれらにアクセスしていたようでした。これは動作しません:

var accessToken =((ClaimsPrincipal)HttpContext.Current.User).FindFirst("AccessToken").Value; 

これがしますが:

var accessToken =((ClaimsIdentity)HttpContext.Current.User.Identity).FindFirst("AccessToken").Value; 
関連する問題