Код: Выделить всё
// Sign-in users with the Microsoft identity platform
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
Configuration.Bind("AzureAd", options);
options.Events.OnTokenValidated = async context =>
{
string tenantId = context.SecurityToken.Claims
.FirstOrDefault(x => x.Type == "tid" || x.Type == "http://schemas.microsoft.com/identity/claims/tenantid")?.Value;
if (string.IsNullOrWhiteSpace(tenantId))
throw new UnauthorizedAccessException("Unable to retrieve tenantId from token.");
var dbContext = context.HttpContext.RequestServices.GetRequiredService();
var authorizedTenant = await dbContext.AuthorizedTenants
.FirstOrDefaultAsync(t => t.TenantId == tenantId);
if (authorizedTenant == null)
throw new UnauthorizedTenantException("This tenant is not authorized.");
};
options.Events.OnAuthenticationFailed = context =>
{
if (context.Exception is UnauthorizedTenantException)
{
context.Response.Redirect("/Home/UnauthorizedTenant");
context.HandleResponse(); // Suppress the exception
}
return Task.CompletedTask;
};
})
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();
Как я могу улучшить свой процесс аутентификации, чтобы гарантировать, что запросы на согласие администратора отправляются только арендаторам, включенным в белый список, и любой арендатор, не внесенный в белый список, полностью заблокирован от дальнейшего взаимодействия?
Подробнее здесь: https://stackoverflow.com/questions/790 ... hitelisted
Мобильная версия