asp.net-core - Azure AD 登录无限循环
问题描述
我的代码正在进入一个无限循环,点击 azure 登录页面(由 Microsoft 托管),然后重定向回我的应用程序,然后返回到 ms 主机登录页面等等等等。
在我的代码中,我在OnAuthorizationCodeReceived
事件中有一个断点......
public void ConfigureAzureAd(IServiceCollection services)
{
//set authentication to use Azure AD
services.AddAuthentication(auth =>
{
auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
auth.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
auth.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(opts =>
{
Configuration.GetSection("OpenIdConnect").Bind(opts);
opts.Events = new OpenIdConnectEvents
{
OnAuthorizationCodeReceived = async ctx =>
{
HttpRequest request = ctx.HttpContext.Request;
//We need to also specify the redirect URL used
string currentUri = UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path);
//Credentials for app itself
var credential = new ClientCredential(ctx.Options.ClientId, ctx.Options.ClientSecret);
//Construct token cache
ITokenCacheFactory cacheFactory = ctx.HttpContext.RequestServices.GetRequiredService<ITokenCacheFactory>();
TokenCache cache = cacheFactory.CreateForUser(ctx.Principal);
var authContext = new AuthenticationContext(ctx.Options.Authority, cache);
//Get token for Microsoft Graph API using the authorization code
string resource = "https://graph.microsoft.com";
AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
ctx.ProtocolMessage.Code, new Uri(currentUri), credential, resource);
//Tell the OIDC middleware we got the tokens, it doesn't need to do anything
ctx.HandleCodeRedemption(result.AccessToken, result.IdToken);
//ctx.HandleCodeRedemption();
}
};
});
}
我可以检查 中的数据result
,一切看起来都很好(虽然不确定失败会是什么样子),看起来登录正在工作,但我的应用程序无法识别登录已经发生,或者它没有保存,并保持重试
我还要求其他人尝试使用不在我的 Active Directory 中的用户登录,但它适当地失败了,看起来 Active Directory 真的很开心,但我的应用程序只是不断重定向。
我正在使用 .Net Core 2.2(我的第一个核心项目)
我正在使用免费的 Active Directory
更新以响应@Marilee Turscak - MSFT
如果我在 portal.azure.com 中没有正确的回复 URL 设置并通过 C# 传递它,那么 azure 会抛出一个错误,所以我肯定有一个回复 URL 并且它匹配正确
配置如下所示:
"OpenIdConnect": {
"ClientId": "<guid in here>", // Application ID
"ClientSecret": "<secrect from portal.azure.com>",
"Authority": "https://login.microsoftonline.com/emailwithout@symbol.onmicrosoft.com/",
"PostLogoutRedirectUri": "http://www.<projectname_in_here>.local",
"CallbackPath": "/signin-oidc",
"ResponseType": "code id_token"
}
解决方案
您需要在代码和 Azure AD 中的应用程序注册中设置回复 URL。您应该将您的回复 URL 设置为您希望将用户重定向到的任何位置(通常是您发布的主要主页 url - 例如https://myapp.azurewebsites.net)。
作为参考,您可以查看 Github 示例中的示例。 https://azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-webapp-openidconnect/
推荐阅读
- python-3.x - 网格搜索分数返回什么值?
- python - 我们在 Django 应用程序中有内存吗?
- apache-camel - Camel:处理文件、拆分和聚合
- c - 为什么我的缓冲区存储在堆栈中的两个位置?
- java - 我们可以修改或自定义 Java 对象的数据结构(例如树、链表等)吗?
- angular - 在 pipe() 中引用 ViewChild
- apache-kafka - KafkaStreams 在拓扑中添加超过 1 个处理器不起作用
- java - 访问 Java hashmap 以获取子类
- kotlin - 如何使用简短形式覆盖属性设置器?
- javascript - 使 SVG 完全填满整个页面