2016-06-09 60 views
1

我花了一些时间让我的MVC 6 .NET Core网站与Azure B2C一起工作,一切似乎都很顺利。然而,围绕这些说法有几个问题,我似乎无法找出正确的策略。使用Azure B2C/.NET Core更新声明

说一个用户在我的网站上注册电子邮件,名字,姓氏。一旦注册完成后,我想添加一条记录到我的数据库中引用此用户的UserProfile表中。

问题1: 我应该在Azure B2C中创建“UserProfileId”声明吗?还是应该在我的数据库表中创建一个引用AD用户的“ObjectId”字段?什么会更有意义?

问题2: 一旦用户注册,我在哪里以及如何更新AD用户声明?我会在其中一个事件中做到吗?或者别的地方?我看到有一个“用户是新的”声明,我可以检查吗?

OnAuthenticationValidated 
OnAuthorizationCodeReceived 
OnRedirectToAuthenticationEndpoint 

问题3: 更新的版权声明,我会用:Microsoft.Azure.ActiveDirectory.GraphClient?有没有人有关于如何更新自定义索赔的示例代码?我试过这个,但它似乎没有坚持:

var identity = context.AuthenticationTicket.Principal.Identity as ClaimsIdentity; 
identity?.AddClaim(new Claim("EmployeeId", "33")); 

这是我的验证配置。谢谢!!!!!

public void ConfigureAuth(IApplicationBuilder app, IOptions<PolicySettings> policySettings, AuthenticationHelper authHelper) 
{ 
    app.UseCookieAuthentication(options => 
    { 
     options.AutomaticAuthenticate = true; 
     options.AutomaticChallenge = true; 
     options.AccessDeniedPath = "/Home/Forbidden"; 
     options.CookieSecure = CookieSecureOption.Always; 
     options.ExpireTimeSpan = TimeSpan.FromHours(1); 
     options.SlidingExpiration = true; 
    }); 

    app.UseOpenIdConnectAuthentication(options => 
    { 
     options.PostLogoutRedirectUri = policySettings.Value.PostLogoutRedirectUri; 
     options.AutomaticAuthenticate = true; 
     options.AutomaticChallenge = true; 
     options.ClientId = policySettings.Value.ClientId; 
     options.CallbackPath = new PathString("/signin-mysite"); 
     options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
     options.Scope.Add("openid"); 
     options.Scope.Add("profile"); 
     options.Scope.Add("email"); 
     options.ResponseType = OpenIdConnectResponseTypes.IdToken; 
     options.Authority = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", policySettings.Value.AadInstance, policySettings.Value.Tenant); 
     options.Events = new OpenIdConnectEvents { 
      OnAuthenticationValidated = OnAuthenticationValidated, 
      OnAuthorizationCodeReceived = OnAuthorizationCodeReceived, 
      OnAuthenticationFailed = OnAuthenticationFailed, 
      OnRedirectToAuthenticationEndpoint = OnRedirectToAuthenticationEndpoint 
     }; 
     options.ConfigurationManager = new PolicyConfigurationManager(
      String.Format(CultureInfo.InvariantCulture, "{0}/{1}/{2}/{3}", policySettings.Value.AadInstance, policySettings.Value.Tenant, "v2.0", OpenIdProviderMetadataNames.Discovery), 
      new string[] { policySettings.Value.SignUpInPolicyId, policySettings.Value.ProfilePolicyId, policySettings.Value.PasswordPolicyId }); 
    }); 
} 

回答

0

问题1:我应该建立在Azure的B2C一个 “UserProfileId” 要求?还是应该在我的数据库表中创建一个引用AD用户的“ObjectId”字段?什么会更有意义?

1a - 我没有添加任何东西给B2C租户。

1b - 我从B2C获取对象ID并将它作为备用密钥存储在我的表中。我的桌子有自己的唯一ID。我是否希望拥有额外的身份提供者,这是必要的。

我只使用B2C的对象ID来查找用户并获取我自己的ID。

问题2:一旦用户注册,在哪里以及如何更新AD用户声明?我会在其中一个事件中做到吗?或者别的地方?我看到有一个“用户是新的”声明,我可以检查吗?

当你说“更新索赔”时,你是指在B2C租户中永久更新,还是你的意思是将其添加到其他索赔并在此特定令牌的生命周期中临时使用它?

没有使用图形客户端,没有连接回B2C。

userIsNew声明来自B2C一次,仅在注册过程结束时。您可以使用它来确定您是否有新用户尝试访问您的系统。我从那些声称B2C给我的声明中创建了新的条目,声明都来自我的表中的信息。

问题3:要更新声明,我会使用:Microsoft.Azure.ActiveDirectory.GraphClient?有没有人有关于如何更新自定义索赔的示例代码?我试过这个,但它似乎并没有坚持:

我必须再次问“更新”问题。

您可能正在寻找的是“转换”的索赔。这通常是在针对cookie的TicketReceived事件期间完成的。当他们第一次有验证时会发生这种情况。 (不要与注册混淆。)

我并不是那么明亮,但我会告诉你,我花了太多时间在这个试图让它正确。主要是因为有大量的选择,没有人可以告诉你所有正确的项目。所以你只需看看你想要找到的那些信息。

我发现this book(它的作者)非常有帮助。这是目前的,他是一个微软的人,写得很好。

HTH

0

关于问题1:我也一样nhwilly:我存储在我的数据库的附加信息。

关于问题2:您可以添加在OnsigningIn事件的声明:

app.UseCookieAuthentication(new Microsoft.AspNetCore.Builder.CookieAuthenticationOptions() 
     { 
      Events = new CookieAuthenticationEvents() 
      { 
       OnSigningIn = (context) => 
       { 
        ClaimsIdentity identity = (ClaimsIdentity)context.Principal.Identity; 
        identity.AddClaim(new Claim("sb:tID", "555")); 
        return Task.FromResult(0); 
       } 
      } 
     }); 

我从Transforming Open Id Connect claims in ASP.Net Core的信息。

关于问题3:我没有做它自己,但这个环节应该让你动工:https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-graph-dotnet

希望帮助!