2016-06-17 60 views
3

Web APIでAzure B2Cを使用した場合の例では、app.UseOAuthBearerAuthentication(下図参照)が表示されますが、ASP .NET 5 Web APIプロジェクトではIApplicationBuilder(IAppBuilderではなく)を使用し、UseOAuthBearerAuthenticationでは存在する。私はapp.UseOpenIdConnectAuthenticationを試しましたが、これはクッキーを使用していると私はそれがクライアントとしてXamarinアプリを使用して動作させることができなかったと信じています。私はapp.UseWindowsAzureActiveDirectoryBearerAuthenticationを試みましたが、これは標準のAzure AD(B2Cではない)のためのものだと思いますか? Azure B2Cを最新のASP .NET Web APIで動作させる方法Azure B2C with Web API

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

public void ConfigureAuth(IAppBuilder app) 
    { 
     TokenValidationParameters tvps = new TokenValidationParameters 
     { 
      // This is where you specify that your API only accepts tokens from its own clients 
      ValidAudience = clientId, 
     }; 

     app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions 
     { 
      // This SecurityTokenProvider fetches the Azure AD B2C metadata & signing keys from the OpenIDConnect metadata endpoint 
      AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider(String.Format(aadInstance, tenant, "v2.0", discoverySuffix, commonPolicy))) 
     }); 
    } 

答えて

5

これは私に役立ちます。 JWTのトークンは、あなたがWeb APIでJwtBearerAuthenticationを使用することができ、あなたのクライアントで利用可能な場合

public void ConfigureAuth(IApplicationBuilder app, IOptions<PolicySettings> policySettings) 
{ 
    app.UseJwtBearerAuthentication(new JwtBearerOptions 
    { 
     AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme, 
     AutomaticAuthenticate = true, 
     AutomaticChallenge = true, 
     MetadataAddress = "https://login.microsoftonline.com/[my-tenant].onmicrosoft.com/v2.0/.well-known/openid-configuration?p=B2C_1_my-signup-signin-policy", 
     Audience = "[My-Azure-App-Guid]", 
     Events = new JwtBearerEvents 
     { 
      OnTokenValidated= ctx => 
      { 
       var nameClaim = ctx.AuthenticationTicket.Principal.FindFirst("name"); 
       if (nameClaim != null) 
       { 
        var claimsIdentity = (System.Security.Claims.ClaimsIdentity)ctx.AuthenticationTicket.Principal.Identity; 
        claimsIdentity.AddClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Name, nameClaim.Value)); 
       } 
       return Task.FromResult(0); 
      }, 
      OnAuthenticationFailed = ctx => 
      { 
       ctx.SkipToNextMiddleware(); 
       return Task.FromResult(0); 
      } 
     } 
    }); 
} 
+0

https://dzimchuk.net/post/setting-up-your-aspnet-core-apps-and-services-for-azure- ad-b2c – aherrick

+0

この回答は.NetCoreを使用しているようです。標準の.Net Jwtライブラリを使用するすべてのソリューション? – petryuno1

0

:私はそれが最新の.NETのWeb APIフレームワークとAzureのB2Cを使用しようとしている他の誰かに役立ちます願っています。 Azure B2C(ソーシャルログイン)によって発行されたJWTは、Web APIで認証されるトークンとして使用できます。

は、クライアントがWebアプリです。このサンプルでhttps://github.com/sendhilkumarg/AzureB2CWebAppAndAPIAuthentication を参照してください