[英]Can I use Azure AD B2c as secondary authentication provider in asp.net core application?
[英]Asp.Net Core 2.0 and Azure AD B2C for authentication on WebApp and API
我有一個用於測試的現有小應用程序,它在Web App和API的Asp.Net Core 1.1中,使用Azure AD B2C進行身份驗證。 我正在嘗試將其移至.Net Core 2.0,但我無法確定如何使其工作,我嘗試使用GitHub Azure-Samples for Web App和API的兩個示例,但我在嘗試時有未經授權或500錯誤訪問api,如果您有一個使用2.0從Web應用程序調用web api並受AD B2C保護的工作示例,將非常感謝。
編輯:我用來測試的示例是:Web App: WebApp-OpenIDConnect-DotNet core2.0 Web Api: B2C-WebApi core2.0 ,我更改了appsettings值以匹配我的b2c目錄。
對於我的asp.net核心1.1測試應用程序,我使用與上面相同的示例,但是來自主分支,具有相同的appsettings值。
默認編輯2 ,在startup.cs我有這個:
services.AddAuthentication()
.AddJwtBearer(option => new JwtBearerOptions
{
Authority = string.Format("https://login.microsoftonline.com/tfp/{0}/{1}/v2.0/",
Configuration["Authentication:AzureAd:Tenant"], Configuration["Authentication:AzureAd:Policy"]),
Audience = Configuration["Authentication:AzureAd:ClientId"],
Events = new JwtBearerEvents
{
OnAuthenticationFailed = AuthenticationFailed
}
});
這給了我以下錯誤:
Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:請求啟動HTTP / 1.1 GET http:// localhost:44352 / api / values / 5
Microsoft.AspNetCore.Server.Kestrel:錯誤:連接ID“0HL89JHF4VBLM”,請求ID“0HL89JHF4VBLM:00000001”:應用程序拋出了未處理的異常。 System.InvalidOperationException:未指定authenticationScheme,並且未找到DefaultChallengeScheme。
如果修改了services.AddAuthentication那樣的話
services.AddAuthentication(sharedOption =>
{
sharedOption.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
錯誤現在
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:信息:無法驗證令牌xxx。 Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException:IDX10500:簽名驗證失敗。 沒有提供安全密鑰來驗證簽名。 at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(String token,TokenValidationParameters validationParameters),位於Microsoft.AspNetCore.Authentication.JwtBearer的System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(String token,TokenValidationParameters validationParameters,SecurityToken和validatedToken)。 JwtBearerHandler.d__6.MoveNext()
我在樣本上看到了一個拉取請求來糾正這個問題( 鏈接 ),services.AddAuthentication必須更改為:
services.AddAuthentication(options =>
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(jwtOptions =>
{
jwtOptions.Authority = $"https://login.microsoftonline.com/tfp/{Configuration["Authentication:AzureAd:Tenant"]}/{Configuration["Authentication:AzureAd:Policy"]}/v2.0/";
jwtOptions.Audience = Configuration["Authentication:AzureAd:ClientId"];
jwtOptions.Events = new JwtBearerEvents
{
OnAuthenticationFailed = AuthenticationFailed
};
});
我得到了這個示例適用於Core 1.1和Core 2.0,請添加Oath身份驗證,如下所示,
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAdB2C(options => Configuration.Bind("Authentication:AzureAdB2C", options))
您將在“AzureAdB2CAuthenticationBuilderExtensions”類中定義配置選項,該類位於azure b2c項目中
看起來您的令牌沒有從Azure更新它,您是否能夠從您的Web應用程序獲取令牌? 你可以驗證你沒有得到null
您是否在azure b2c租戶網絡應用程序上注冊了api范圍? “ApiScopes”:“ https://fabrikamb2c.onmicrosoft.com/demoapi/demo.read ”
你必須在web api中設置范圍並允許在網絡應用上閱讀,請點擊鏈接以設置它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.