2016-09-22 67 views
0

我使用Open NET Connect标准与ASP NET MVC网站实现了一个示例应用程序。我的目标是将存储敏感数据外包给Azure,因此我使用Azure Active Directory。由于无法向Azure中的用户添加自定义属性,因此我们将非敏感用户声明存储在我们的专用数据库中。我设法得到这种权利,并将其“添加”到cookie这样的:OpenID Connect ASP NET MVC AAD

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中,所以它会持续下去。理想情况下,我想以某种方式强制

AuthorizationCodeReceived

功能再次调用。可能吗 ?或者有另一种方法可以将cookie中存储的值交换出来吗?

到目前为止,我唯一的解决方案是在用户更改此值时注销用户,这样会强制他再次注销,并且我将再次调用AuthorizationCodeReceived的回调,但这不是一种非常用户友好的方式。

回答

0

在标识对象中添加声明以在Cookie中保留新声明后,可以调用HttpContext.GetOwinContext()。Authentication.SignIn()。

+0

会试着让你知道。 – MajkeloDev

+0

我试过了。它没有工作,它没有调用AuthorizationCodeReceived,但这工作:HttpContext.GetOwinContext()。Authentication.Challenge(new AuthenticationProperties {RedirectUri =“/”},OpenIdConnectAuthenticationDefaults.AuthenticationType); – MajkeloDev

+0

用我的代码推荐你的答案,我会将其标记为完成。有人可以找到它有用:) – MajkeloDev