В моем Startup.cs я настраиваю соединение openid, как описано в Microsoft Learn:
Код: Выделить всё
public void ConfigureServices(IServiceCollection services)
{
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddControllersWithViews();
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(options =>
{
IConfigurationSection sectionOptions = AppConfiguration?.GetSection("OpenIdConnectOptions");
if(sectionOptions?.GetChildren()?.Any() == true)
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = sectionOptions["Authority"];
options.ClientId = sectionOptions["ClientId"];
options.ClientSecret = sectionOptions["ClientSecret"];
options.CallbackPath = new PathString(sectionOptions["CallbackPath"]);
options.SignedOutCallbackPath = new PathString(sectionOptions["SignedOutCallbackPath"]);
options.RemoteSignOutPath = new PathString(sectionOptions["RemoteSignOutPath"]);
options.ResponseType = OpenIdConnectResponseType.Code;
options.ResponseMode = OpenIdConnectResponseMode.Query;
options.GetClaimsFromUserInfoEndpoint = true;
List scopes =
(from curSubsectionScope in sectionOptions.GetSection("Scope")?.GetChildren()
where !string.IsNullOrWhiteSpace(curSubsectionScope.Value)
select curSubsectionScope.Value.Trim().ToLower())?.ToList();
options.Scope?.Clear();
scopes?.ForEach(options.Scope.Add);
options.MapInboundClaims = false;
options.TokenValidationParameters.NameClaimType = JwtRegisteredClaimNames.Name;
options.TokenValidationParameters.RoleClaimType = ClaimTypes.Role;
}
}
}
Код: Выделить всё
"OpenIdConnectOptions": {
"Authority": "https://login.microsoftonline.com/{my-tenant-id}/v2.0/",
"ClientId": "{my-client-id}",
"ClientSecret": "{my-client-secret}",
"CallbackPath": "/signin-oidc",
"SignedOutCallbackPath": "/signout-callback-oidc",
"RemoteSignOutPath": "/signout-oidc",
"RedirectUri": "http://localhost:80/azure",
"SaveTokens": true,
"Scope": [ "openid", "offline_access" ]
},
Код: Выделить всё
az ad app show --id $clientId --query "web.redirectUris"
[
"https://localhost/signin-oidc",
"https://localhost/signout-callback-oidc",
"https://localhost/signout-oidc",
"http://localhost/azure"
]
az ad app permission list --id $clientId
[
{
"resourceAccess": [
{
"id": "e1fe6dd8-ba31-4d61-89e7-88639da4683d", # User.Read
"type": "Scope"
},
{
"id": "1bfefb4e-e0b5-418b-a88f-73c46d2cc8e9", # Application.ReadWrite.All
"type": "Role"
},
{
"id": "37f7f235-527c-4136-accd-4a02d197296e", # openid
"type": "Scope"
},
{
"id": "7427e0e9-2fba-42fe-b0c0-848c9e6a8182", # offline_access
"type": "Scope"
}
],
"resourceAppId": "00000003-0000-0000-c000-000000000000" # https://graph.microsoft.com
}
]
Код: Выделить всё
string access_token = await httpContext.GetTokenAsync(OpenIdConnectParameterNames.AccessToken);
string id_token = await httpContext.GetTokenAsync(OpenIdConnectParameterNames.IdToken);
Что не так в моих настройках?
Подробнее здесь: https://stackoverflow.com/questions/786 ... id-connect