【发布时间】:2020-06-27 21:49:22
【问题描述】:
我正在尝试将 Azure AD 身份验证添加到当前使用单个用户帐户的 .Net MVC 应用程序。我已经在 Azure 中设置了应用注册,并安装并配置了 OpenID Connect。
在 Startup.cs 我添加了:
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
// app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
// Sets the ClientId, authority, RedirectUri as obtained from web.config
ClientId = clientId,
Authority = authority,
RedirectUri = redirectUri,
// PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page
PostLogoutRedirectUri = redirectUri,
Scope = OpenIdConnectScope.OpenIdProfile,
// ResponseType is set to request the id_token - which contains basic information about the signed-in user
ResponseType = OpenIdConnectResponseType.IdToken,
// ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application
// To only allow users from a single organizations, set ValidateIssuer to true and 'tenant' setting in web.config to the tenant name
// To allow users from only a list of specific organizations, set ValidateIssuer to true and use ValidIssuers parameter
TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true // Simplification (see note below)
},
// OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed
}
}
);
在web.config 中,我将身份验证类型设置为无
<authentication mode="None"/>
然后我添加了一个动作方法来触发挑战:
if (!Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties { RedirectUri = "http://localhost:54465" },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
我仍然可以使用个人用户帐户登录应用程序(我想保留此选项),但无法使用 Azure AD 登录。用户被重定向到 Azure AD 登录屏幕并登录,我可以看到返回带有 id_token 的响应,但从未在应用程序中进行身份验证。
我知道这应该在 OpenID Connect 中间件中自动发生,但是还有什么需要设置的吗?
【问题讨论】:
标签: asp.net-mvc azure-active-directory openid-connect