2017-05-04 5 views
0

OAPuthでADFS認証を使用して、WebアプリケーションとWebAPIの間で通信しようとしています。私はADFS4を使用しており、それに応じてサーバーアプリケーションとWebapiでアプリケーショングループを構成しました。私はuserdetails、特にwebapiコントローラからユーザ名を受け取ろうとしています。 webapiに渡されるアクセストークン内のユーザ名の詳細を渡すことは可能ですか?ここで私はのWebapp側からやったことです:WEBAPI端から、ADFS認証後にWebアプリケーションのコントローラでADAP4-OAuth2のWebAPIからユーザー名を取得できません。OpenID

authContext = new AuthenticationContext(Startup.authority, false); 
ClientCredential credential = new ClientCredential(Startup.clientId, Startup.appKey); 
string accessToken = null; 
bool isAuthenticated = User.Identity.IsAuthenticated; //return true 
string username = User.Identity.Name; // returns username 
string userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Name).Value; // returns username 

HttpClient httpClient = new HttpClient(); 
try 
{ 
    result = authContext.AcquireTokenAsync(Startup.apiResourceId, credential).Result; 
    accessToken = result.AccessToken; 

} 
catch (AdalException ex) 
{ 
} 

httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); 

HttpResponseMessage response = httpClient.GetAsync(Startup.apiResourceId + "/api/ConfApi").Result; 

、Startup.Auth.csに、私はこれらのコード

public void ConfigureAuth(IAppBuilder app) 
{ 
    JwtSecurityTokenHandler.InboundClaimTypeMap.Clear(); 
    app.UseActiveDirectoryFederationServicesBearerAuthentication(
     new ActiveDirectoryFederationServicesBearerAuthenticationOptions 
     { 
      MetadataEndpoint = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"], 
      TokenValidationParameters = new TokenValidationParameters() { 
       SaveSigninToken = true, 
       ValidAudience = ConfigurationManager.AppSettings["ida:Audience"] 
      } 
     }); 
    } 
を追加しました

しかし、ConfApiコントローラでは、ユーザーの詳細を含むクレームが見つかりません。

Webapiコントローラでユーザーの詳細を受け取るにはどうすればよいですか?

ありがとうございました。

+0

[MSIS9649:無効なOAuth要求を受信しました。 'assertion'パラメータの値が有効なアクセストークンではありません](https:// stackoverflow。com/questions/43867426/msis9649-received-invalid-oauth-request-the-assertion-parameter-value-is-not) –

答えて

0

実際に申し立てを受けていますか?

ADFS側でWeb APIのクレームルールを設定しましたか?

名前 - 指定名、表示名などには何を使用しましたか?

トラフィックを監視するためにFiddlerのようなものを使用してください。 OIDC認証後、アクセストークン、IDトークンなどが表示されます。

トークンを取り、jwt.ioにコピーします。

実際に受け取った内容が表示されます。

しかし、OWINクラスは単純なOAuth属性を変換します。クレームタイプURIに「aud」を付けます。 http://claims/this-claimブレークポイントを開き、クレームコレクションにあるものとそれぞれに割り当てられているタイプを確認します。

0

これに対する答えは、同じ質問への答えです:あなたは(この場合はWebアプリケーション)サーバアプリを取得する(代わりに、クライアントの資格情報が流れを付与)認証コードの流れを使用する必要がMSIS9649: Received invalid OAuth request. The 'assertion' parameter value is not a valid access token

ユーザーのコンテキストでWeb APIと対話します。認可コードフローは、JWTトークンのクレームを通過します。 Web APIのRPTクレーム発行変換ルールで、Web APIに必要なクレームを必ず渡してください。

Vittorio has a nice post on authorization code flow, although it talks about azure.

認証コードフローを使用するためには、Startup.ConfigureAuth(IAppBuilderアプリ)からOpenIdConnectAuthenticationOptionsの通知を経由してAuthorizationCodeReceivedイベントを処理する必要が

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions { 
     ... 
     Notifications = new OpenIdConnectAuthenticationNotifications { 
      AuthorizationCodeReceived = async code => { 
       ClientCredential credential = new ClientCredential(Startup.clientId, Startup.appKey); 
       AuthenticationContext authContext = new AuthenticationContext(Startup.authority, false); 
       AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
        code.Code, 
        new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), 
        credential, 
        Startup.apiResourceId); 
      } 
     } 

あなたが作るする準備ができたら、あなたはトークンを黙って取得します。

var authContext = new AuthenticationContext(Startup.authority, false); 
var credential = new ClientCredential(Startup.clientId, Startup.appKey); 
var claim = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; 
var userId = new UserIdentifier(claim, UserIdentifierType.UniqueId); 

result = await authContext.AcquireTokenSilentAsync(
    Startup.apiResourceId, 
    credential, 
    userId); 

HttpClient httpClient = new HttpClient(); 
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
    "Bearer", 
    result.AccessToken); 
関連する問題