2016-03-02 80 views
2

我正在使用注册策略帮助创建B2C用户,但我们需要向此新用户添加一些用户属性(扩展属性)。例如为用户设置“AccountId”。将参数传递给注册策略

如果我添加“AccountId”作为注册属性,并输入一些值,它工作正常,当我通过Graph API检查用户属性时,“AccountId”是正确的。

enter image description here enter image description here

但是,在这种情况下,“ACCOUNTID”应该为可编辑或对用户可见,我只是想在注册政策,加上“ACCOUNTID”与例如Facebook的创建的用户,作为注册页面上的隐藏字段。

从我使用Azure B2C AD的ASP.Net MVC应用程序中,是否可以将此值传递给注册页面并将其与注册属性相关联?它可以通过参数(& accountid = 1234)或某些OpenId属性完成吗?

回答

1

Azure AD B2C确实不是当前接受任何用于填充用户配置文件属性的额外查询字符串参数。您可以在Azure AD B2C UserVoice forum中提出此要求。

但是,您可以通过在使用Graph的应用程序中自己实现相同的结果。

对于您的具体示例,您需要确保您发送配置注册或注册/签名策略来发送newUser声明,然后在验证后使用该策略调用图形并进行必要的更新。

这里是你如何做到这一点的例子,假设你使用ASP.Net按this SignIn samplethis SignUp/SignIn sample,通过利用SecurityTokenValidated通知设置您的OpenIdConnectAuthenticationOptions像这样:

new OpenIdConnectAuthenticationOptions 
{ 
    // Skipping for brevity 
    // (...) 
    Notifications = new OpenIdConnectAuthenticationNotifications 
    { 
    // (...) 
    SecurityTokenValidated = OnSecurityTokenValidated 
    }, 
    // (...) 
}; 

而且使用在ClientCredentials流向调出该图形API进行更新,像这样:

private async Task OnSecurityTokenValidated(SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification) 
{ 
    string userObjectId = notification.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value; 
    bool newUser = false; 
    bool.TryParse(notification.AuthenticationTicket.Identity.FindFirst("newUser")?.Value, out newUser); 

    if (!newUser) return; 

    ClientCredential credential = new ClientCredential(graphClientId, graphClientSecret); 
    AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/sacacorpb2c.onmicrosoft.com"); 

    AuthenticationResult result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", credential); 

    string body = "{ \"extension_e5bf5a2db0c9415cb62661a70d8f0a68_AccountId\" : \"Your_New_Value"\"}"; 

    HttpClient http = new HttpClient(); 
    string url = "https://graph.microsoft.com/beta/users/" + userObjectId + "/"; 
    HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), url) 
    { 
    Content = new StringContent(body, Encoding.UTF8, "application/json") 
    }; 
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); 
    HttpResponseMessage response = await http.SendAsync(request); 

    return; 
} 

重要提示:

  • 如果要更新内置属性,可以使用Azure AD图表(https://graph.windows.net),但是如果您想更新自定义属性,则需要查询Microsoft Graph(https://graph.microsoft.com)的Beta端点。如果您确实需要自定义属性,请注意它们有更好的名称(前缀为Guids),请使用Graph Explorer,query/beta/users并查看完整的属性名称。
  • 您需要注册一个单独的(来自您正在使用的用于登录/注册的应用程序)应用程序,并且有权与Graph交谈。有关更多信息,请参阅this article,但不是该文章请求Azure AD图的许可权,您可能需要根据以前的观点获取Microsoft Graph的权限。