将 Azure AD 身份验证添加到现有的 .Net MVC 应用程序:id_token 未验证
Adding Azure AD authentication to existing .Net MVC app: id_token not being validated
我正在尝试将 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
中我将身份验证类型设置为 None
<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 中间件中自动发生,但是还有什么需要设置的吗?
记得设置 RedirectUri
与你在 azure ad 中注册的应用相同。
我用你的代码进行了测试,可以在应用程序中进行身份验证。在 Azure 广告应用程序注册中,单击 Authentication
并选择 Id Token
。
您可以参考此 article 并下载有关将 Microsoft 身份平台登录添加到 ASP.NET 网络应用的示例。
我使用相同的 Azure 应用程序注册创建了一个干净的项目并且它按预期工作,因此开始将干净的项目与我的旧项目进行比较。在项目的早期,它使用了 SimpleMembershipProvider,即使此后项目已更改为使用 ASP.Net Identity,WebMatrix dll 仍被引用并包含在构建中。
删除这些引用、清理和重新构建已解决问题。
我正在尝试将 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
中我将身份验证类型设置为 None
<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 中间件中自动发生,但是还有什么需要设置的吗?
记得设置 RedirectUri
与你在 azure ad 中注册的应用相同。
我用你的代码进行了测试,可以在应用程序中进行身份验证。在 Azure 广告应用程序注册中,单击 Authentication
并选择 Id Token
。
您可以参考此 article 并下载有关将 Microsoft 身份平台登录添加到 ASP.NET 网络应用的示例。
我使用相同的 Azure 应用程序注册创建了一个干净的项目并且它按预期工作,因此开始将干净的项目与我的旧项目进行比较。在项目的早期,它使用了 SimpleMembershipProvider,即使此后项目已更改为使用 ASP.Net Identity,WebMatrix dll 仍被引用并包含在构建中。
删除这些引用、清理和重新构建已解决问题。