带有Graph API的Azure AD B2C - 如何获取/设置用户的电子邮件?

时间:2017-10-10 16:03:55

标签: azure-active-directory azure-ad-b2c azure-ad-graph-api

我使用Graph API将用户添加到Azure AD B2C,但我不知道如何存储用户的电子邮件(主要的)。哪个字段 here 是用户的主电子邮件地址?

正如我在此处阅读的那样,无法在身份验证联系信息中填充值。这是正确的吗?

2 个答案:

答案 0 :(得分:0)

您需要向用户端点发送PATCH请求

{baseurl}/{tenantId}/users?api-version={apiVersion}

不要忘记你在auth标题中访问令牌:

Authorization: Bearer {accessToken}

这是一个示例模型(Java),其中包含用于计算和设置用户对象上的登录电子邮件的方法:

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class GraphApiUserExample{

    @JsonProperty("objectId")
    private String id;

    private Boolean accountEnabled;

    private PasswordProfile PasswordProfile;

    private List<SignInName> signInNames;

    private String surname;

    private String displayName;

    private String givenName;

    @JsonProperty("userPrincipalName")
    private String userPrincipalName;

    public String getId(){

        return id;
    }

    public void setId(final String id){

        this.id = id;
    }

    public Boolean getAccountEnabled(){

        return accountEnabled;
    }

    public void setAccountEnabled(final Boolean accountEnabled){

        this.accountEnabled = accountEnabled;
    }

    public PasswordProfile getPasswordProfile(){

        return passwordProfile;
    }

    public void setPasswordProfile(final PasswordProfile passwordProfile){

        this.passwordProfile = passwordProfile;
    }

    public List<SignInName> getSignInNames(){

        return signInNames;
    }

    public void setSignInNames(final List<SignInName> signInNames){

        this.signInNames = signInNames;
    }

    public String getSurname(){

        return surname;
    }

    public void setSurname(final String surname){

        this.surname = surname;
    }

    public String getDisplayName(){

        return displayName;
    }

    public void setDisplayName(final String displayName){

        this.displayName = displayName;
    }

    public String getGivenName(){

        return givenName;
    }

    public void setGivenName(final String givenName){

        this.givenName = givenName;
    }

    public String getUserPrincipalName(){

        return userPrincipalName;
    }

    public void setUserPrincipalName(final String userPrincipalName){

        this.userPrincipalName = userPrincipalName;
    }

    @JsonIgnore
    public String getSignInEmail(){

        String email = "";
        if(signInNames != null){
            for(SignInName signInName : signInNames){
                if(signInName.getType().equals("emailAddress")){
                    email = signInName.getValue();
                    break;
                }
            }
        }
        return email;
    }

    @JsonIgnore
    public void setSignInEmail(String signInEmail){

        if(signInNames == null){
            signInNames = new ArrayList<>();
            signInNames.add(new SignInName("emailAddress", signInEmail));
            return;
        }

        for(SignInName signInName : signInNames){
            if(signInName.getType().equals("emailAddress")){
                signInName.setValue(signInEmail);
                break;
            }
        }
    }
}

SignInName:

public class SignInName {//userName or emailAddress

    private String
        type,
        value;

    public String getType(){

        return type;
    }

    public void setType(final String type){

        this.type = type;
    }

    public String getValue(){

        return value;
    }

    public void setValue(final String value){

        this.value = value;
    }
}

PasswordProfile:

@JsonIgnoreProperties(ignoreUnknown = true)
public class PasswordProfile {

    private String password;

    private Boolean forceChangePasswordNextLogin;

    public String getPassword(){

        return password;
    }

    public void setPassword(final String password){

        this.password = password;
    }

    public Boolean getForceChangePasswordNextLogin(){

        return forceChangePasswordNextLogin;
    }

    public void setForceChangePasswordNextLogin(final Boolean forceChangePasswordNextLogin){

        this.forceChangePasswordNextLogin = forceChangePasswordNextLogin;
    }
}

答案 1 :(得分:0)

我是这样做的:

public async Task<AdUser> GetUserByObjectId(Guid objectId)
{
    string userJson = await SendGraphGetRequest("/users/" + objectId, null);
    JObject jUser = JObject.Parse(userJson);
    return new AdUser(jUser);
}

internal AdUser(JObject jUser)
{
    AccountEnabled = jUser["accountEnabled"].Value<bool>();
    CompanyName = jUser["companyName"].Value<string>();
    Department = jUser["department"].Value<string>();
    DisplayName = jUser["displayName"].Value<string>();
    FirstName = jUser["givenName"].Value<string>();
    JobTitle = jUser["jobTitle"].Value<string>();
    LastName = jUser["surname"].Value<string>();
    MailNickname = jUser["mailNickname"].Value<string>();
    Mobile = jUser["mobile"].Value<string>();
    ObjectId = new Guid(jUser["objectId"].Value<string>());

    List<string> mailList = new List<string>(jUser["otherMails"].Count());
    mailList.AddRange(jUser["otherMails"].Select(mail => mail.Value<string>()));
    OtherMails = mailList.AsReadOnly();

    Phone = jUser["telephoneNumber"].Value<string>();

    List<(string type, string value)> signInNames = jUser["signInNames"].Select(jToken => (jToken["type"].Value<string>(), jToken["value"].Value<string>())).ToList();
    SignInNames = signInNames.AsReadOnly();

    UserPrincipalName = jUser["userPrincipalName"].Value<string>();
    UserType = jUser["userType"].Value<string>();
}

这是AdUser的电子邮件属性:

public string Email
{
    get
    {
        if (SignInNames.Count > 0 && SignInNames[0].type == "emailAddress")
            return SignInNames[0].value;
        if (OtherMails.Count > 0)
            return OtherMails[0];
        throw new InvalidOperationException("Don't know where to get user Email");
    }
}