作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的 Dotnet 核心 Web 应用程序中使用 Azure AD 登录,它应该触发 OnAuthorizationCodeReceived 事件,但它没有被触发。
能否请您说明一下?
public void Configure(string name, OpenIdConnectOptions options)
{
JsonFileConfigurationService config = new JsonFileConfigurationService();
options.ClientId = config.AzureAdClientId;
options.Authority = $"{config.AzureAdInstance}{config.AzureAdTenantId}";
options.UseTokenLifetime = true;
options.CallbackPath = config.AzureAdCallbackPath;
options.RequireHttpsMetadata = false;
var allScopes = $"{_azureOptions.Scopes} {_azureOptions.GraphScopes}".Split(new[] { ' ' });
foreach (var scope in allScopes) { options.Scope.Add(scope); }
options.TokenValidationParameters = new TokenValidationParameters
{
// Instead of using the default validation (validating against a single issuer value, as we do in line of business apps),
// we inject our own multitenant validation logic
ValidateIssuer = false,
// If the app is meant to be accessed by entire organizations, add your issuer validation logic here.
//IssuerValidator = (issuer, securityToken, validationParameters) => {
// if (myIssuerValidationLogic(issuer)) return issuer;
//}
};
options.Events = new OpenIdConnectEvents
{
OnTicketReceived = context =>
{
// If your authentication logic is based on users then add your logic here
return Task.CompletedTask;
},
OnAuthenticationFailed = context =>
{
context.Response.Redirect("/Home/Error");
context.HandleResponse(); // Suppress the exception
return Task.CompletedTask;
},
OnAuthorizationCodeReceived = async (context) =>
{
var code = context.ProtocolMessage.Code;
var identifier = context.Principal.FindFirst(config.AzureAdObjectIdentifierType).Value;
var memoryCache = context.HttpContext.RequestServices.GetRequiredService<IMemoryCache>();
var graphScopes = _azureOptions.GraphScopes.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var cca = new ConfidentialClientApplication(
_azureOptions.ClientId,
_azureOptions.BaseUrl + _azureOptions.CallbackPath,
new ClientCredential(_azureOptions.ClientSecret),
new SessionTokenCache(identifier, memoryCache).GetCacheInstance(),
null);
var result = await cca.AcquireTokenByAuthorizationCodeAsync(code, graphScopes);
// Check whether the login is from the MSA tenant.
// The sample uses this attribute to disable UI buttons for unsupported operations when the user is logged in with an MSA account.
var currentTenantId = context.Principal.FindFirst(config.AzureAdTenantId).Value;
if (currentTenantId == "9188040d-6c67-4c5b-b112-36a304b66dad")
{
// MSA (Microsoft Account) is used to log in
}
context.HandleCodeRedemption(result.AccessToken, result.IdToken);
},
// If your application needs to do authenticate single users, add your user validation below.
//OnTokenValidated = context =>
//{
// return myUserValidationLogic(context.Ticket.Principal);
//}
};
}
最佳答案
您应该使用混合模式并将响应类型设置为“code id_token”,默认为“id_token”:
options.ResponseType = OpenIdConnectResponseType.CodeIdToken
关于c# - OpenIdConnectEvents OnAuthorizationCodeReceived 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50209838/
我在我的 Dotnet 核心 Web 应用程序中使用 Azure AD 登录,它应该触发 OnAuthorizationCodeReceived 事件,但它没有被触发。 能否请您说明一下? publ
使用 asp.net core 2.2,我在下面的启动中有以下内容我到达 OnRedirectToIdentityProvider 断点,然后我到达 appsettings "CallbackPath
我正在使用 IdentityServer4 和 OpenId 对我的 MVC 应用程序中的用户进行身份验证,并想添加我自己的声明。但我不确定我应该在哪个 OpenIdConnectEvents 中执行
我是一名优秀的程序员,十分优秀!