![](/img/trans.png)
[英]Validating a JWT from AzureAD using Microsoft.Identity.Web succeeds but then fails in the same call
[英]How to request email scope from AAD using Microsoft.Identity.Web
我正在嘗試做一些我知道如何在 .NET Framework in .NET Core 中做的事情,但缺少一個簡單的配置步驟。
.NET 框架:
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Scope = OpenIdConnectScope.OpenIdProfile + " " + OpenIdConnectScope.Email,
....
在 .NET Core 中,我使用的是 Microsoft.Identity.Web 並且在發送 Azure Active Directory 登錄請求之前無法找到設置范圍的位置。 如果我在登錄頁面出現時手動編輯 URL,我可以通過將 email 添加到 URL 查詢字符串中的 scope 來獲得 email 聲明。
這是新應用程序中的 Startup.cs 代碼,我認為這是我需要添加 scope 的地方:
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
作為猜測,我嘗試在此處將“Scopes”添加到我的應用程序設置中:
"AzureAd": {
"Instance": [...]
"Scopes": "openid profile email"
},
但這似乎並沒有附加任何東西。
閱讀 kavyasaraboju-MT 的有用答案后有效的方法:
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme).AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.Scope.Add("email");
});
請檢查以下參考資料是否有幫助。
在啟動配置中(添加 email 范圍)
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
options.Authority += "/v2.0/";
..
//we can add scopes this way
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
//
…
}
在manifest
中,我們可以添加可選聲明(用於訪問令牌或 ID 令牌)
"optionalClaims": {
"idToken": [],
"accessToken": [
{
"name": "email",
"source": null,
"essential": false,
"additionalProperties": []
} ],
在門戶中為 email、配置文件和 openid 訪問權限授予應用程序權限並授予同意。
並確保登錄請求具有必需的范圍
&response_type=id_token &scope=openid%20profile%20email
請參考這些
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.