2016-03-02 86 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的權限。