【发布时间】:2021-05-14 13:41:30
【问题描述】:
我有一个 ASP.NET MVC 框架 Web 应用程序。我想同时使用 .net 身份和 OpenId Connect 进行身份验证(Microsoft 帐户)。
它可以正常工作并根据需要重定向到另一个控制器。在这个目标控制器中,我从声明中获取信息(从 Azure AD 返回)。
我想要从 Azure 向此集合添加更多声明,或者根据需要创建一组新声明。我将声明设置如下,但是当调试器命中另一个控制器时,我只看到 Azure AD 返回的默认声明;我的修改未反映在声明集合中。
如何添加可用于 OpenId Connect (Microsoft) 和 .NET 身份验证的声明?
这就是我在控制器中设置示例声明的方式:
var identity = new ClaimsIdentity("ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
identity.AddClaim(new Claim("test", "test"));
IAuthenticationManager authenticationManager = HttpContext.GetOwinContext().Authentication;
authenticationManager.SignOut("ApplicationCookie");
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);
这是我在Startup中的配置方式:
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,
// 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 code id_token - which contains basic information about the signed-in user
ResponseType = OpenIdConnectResponseType.CodeIdToken,
AuthenticationMode = AuthenticationMode.Passive,
// OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed
},
TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
RoleClaimType = System.Security.Claims.ClaimTypes.Role,
ValidateIssuer = false
}
});
【问题讨论】:
-
我是否了解在您的应用程序中,您希望将声明添加到从 Azure AD 的 ID 令牌中返回的任何声明?如果是这样,这不是正确的方法 - 授权服务器声明声明,授权服务器向您和令牌的其他接收者保证,它已经验证了用户并验证了声明。在您的应用程序中,您可以添加有关用户的更多信息,但不能修改来自 AS 的声明。另外,我看到您在代码中禁用了颁发者验证 - 这不安全。您应该始终验证您的令牌的发行者。
标签: c# asp.net-mvc asp.net-identity openid-connect