我的代码进入一个无限循环,点击azure登录页面(由微软托管),然后重定向回我的应用程序,然后回到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 Core2.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"
}
发布于 2019-01-04 03:35:06
你需要在你的代码和Azure AD中的应用程序注册中设置回复URL。您应该将回复URL设置为您希望用户重定向的位置(通常是您发布的主要主页的url,类似于https://myapp.azurewebsites.net)。
作为参考,您可以查看Github示例中的示例。https://azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-webapp-openidconnect/
发布于 2019-01-09 01:06:02
回答我自己的问题。
我想我的问题在某种程度上与功能文件夹有关。我正在实现自定义路由以启用功能文件夹,但Azure AD代码将其自己的自定义路由设置为"/signin-oidc“。我通过使用Visual Studio使用Azure Active Directory向导创建一个新项目,获得了测试项目登录,但当我将旧代码移植到新的测试项目时,我得到了完全相同的错误,但在新的"Visual Studio向导“中只有很少的配置,它与我的Azure AD连接,注册应用程序并添加所有必需的配置,所以我在添加功能文件夹之前就知道它会这样做,并在功能文件夹之后生成完全相同的错误行为,因此得出结论,这与功能文件夹自定义路由有关。
我找到的代码的Url,如果有人感兴趣的话,它可以帮助实现功能文件夹:https://github.com/ardalis/OrganizingAspNetCore/tree/master/CoreFeatureFolders
https://stackoverflow.com/questions/54026334
复制相似问题