OpenID 连接 ASP NET MVC AAD
OpenID Connect ASP NET MVC AAD
我使用 OpenID Connect 标准和 ASP NET MVC 网站实现了一个示例应用程序。我的目标是将敏感数据的存储外包给 Azure,因此我使用了 Azure Active Directory。由于不可能向 Azure 中的用户添加自定义属性,因此我将非敏感用户声明存储在我们的私有数据库中。我设法得到了这个声明并将它们 "add" 放到 cookie 中,如下所示:
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
RedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthorizationCodeReceived = context =>
{
var objectId = context.AuthenticationTicket.Identity.Claims.First(x => x.Type == "http://schemas.microsoft.com/identity/claims/objectidentifier");
var claims = GetUserClaims(objectId.Value);
foreach (var item in claims)
{
context.AuthenticationTicket.Identity.AddClaim(new System.Security.Claims.Claim(item.Key, item.Value));
}
return Task.FromResult(0);
}
}
通过这种方式,我向 cookie 添加了必需的声明,以便这些声明在我的用户对象中持续存在,直到用户注销为止,这很好,但是有一个声明可以在会话期间更改(基本上用户可以在一页上更改它).问题是我找不到如何在 cookie 中 "change" 这个声明,所以它会持续存在。理想情况下,我想以某种方式强制
AuthorizationCodeReceived
要再次调用的函数。可能吗 ?还是有另一种方法可以交换存储在 cookie 中的值?
到目前为止,我唯一的解决方案是在用户更改此值时注销用户,这样会强制他再次注销,并且我对 AuthorizationCodeReceived 的回调将再次被调用,但这不是一种非常用户友好的方式.
您可以在身份对象中添加声明后调用 HttpContext.GetOwinContext()。Authentication.SignIn() 以将新声明保存在 cookie 中。
我使用 OpenID Connect 标准和 ASP NET MVC 网站实现了一个示例应用程序。我的目标是将敏感数据的存储外包给 Azure,因此我使用了 Azure Active Directory。由于不可能向 Azure 中的用户添加自定义属性,因此我将非敏感用户声明存储在我们的私有数据库中。我设法得到了这个声明并将它们 "add" 放到 cookie 中,如下所示:
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
RedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthorizationCodeReceived = context =>
{
var objectId = context.AuthenticationTicket.Identity.Claims.First(x => x.Type == "http://schemas.microsoft.com/identity/claims/objectidentifier");
var claims = GetUserClaims(objectId.Value);
foreach (var item in claims)
{
context.AuthenticationTicket.Identity.AddClaim(new System.Security.Claims.Claim(item.Key, item.Value));
}
return Task.FromResult(0);
}
}
通过这种方式,我向 cookie 添加了必需的声明,以便这些声明在我的用户对象中持续存在,直到用户注销为止,这很好,但是有一个声明可以在会话期间更改(基本上用户可以在一页上更改它).问题是我找不到如何在 cookie 中 "change" 这个声明,所以它会持续存在。理想情况下,我想以某种方式强制
AuthorizationCodeReceived
要再次调用的函数。可能吗 ?还是有另一种方法可以交换存储在 cookie 中的值?
到目前为止,我唯一的解决方案是在用户更改此值时注销用户,这样会强制他再次注销,并且我对 AuthorizationCodeReceived 的回调将再次被调用,但这不是一种非常用户友好的方式.
您可以在身份对象中添加声明后调用 HttpContext.GetOwinContext()。Authentication.SignIn() 以将新声明保存在 cookie 中。