我们可以使用Gmail地址创建本地帐户用户吗?

时间:2019-05-30 09:31:18

标签: c# azure-active-directory microsoft-graph microsoft-graph-sdks

我们正在使用ADAL.Net在Azure AD中创建用户。使用public class LoggerManager<TCategoryName> : ILoggerManager<TCategoryName> { private readonly ILogger<TCategoryName> _logger; public LoggerManager(ILogger<TCategoryName> logger) { this._logger = logger; } public void LogError(string message, LoggerErrorType type) { var errorLogger = NLog.LogManager.GetLogger(loggerName); errorLogger.Error(message); } } class A { ....DI public void Test() { _loggerManager.LogError("test message") } } UT: public void TestUT() { var loggerMock = new Mock ILoggerManager A>>(); var service = ServiceFactory.Create<A>(loggerMock); service.Test(); //how to Verify logerror message? } ,我们可以提供任何电子邮件地址(gmail或非域电子邮件)作为用户名来创建Azure AD Local帐户。

当我们使用Microsoft Graph(MSAL.Net)尝试相同操作时,我们无法创建用户:

SinginNames

我们如何使用Microsoft Graph或Microsoft Graph Client Library创建gmail地址作为用户名?

新创建的帐户应该是本地用户帐户,而不是访客用户。

Code: Request_BadRequest
Message: Property userPrincipalName is invalid.

2 个答案:

答案 0 :(得分:0)

否您不能。您必须通过以下示例向tenant specific email请求:

请求网址: https://graph.microsoft.com/v1.0/users

{
  "accountEnabled": true,
  "displayName": "KironTestDisplayName",
  "mailNickname": "KironTestNickName",
  "userPrincipalName": "KironTestingCreateUserWithMember@MyTenant.onmicrosoft.com",
  "userType":"guest",
  "passwordProfile" : {
    "forceChangePasswordNextSignIn": true,
    "password": "Test@pass420"
  }
}

要记住的要点:

  1. 获取要在其中创建用户的租户的正确令牌
  2. 需要request permission
  3. userPrincipalName应该遵循 UserName@tenant-value.onmicrosoft.com

  4. “ userType”:“访客”或“成员”您可以添加

  

注意:邮件应类似于myUser@Mytenant.onmicrosoft.com,否则您将遇到类似Property userPrincipalName is invalid的400请求错误

您的案例:

如果您想使用gmail account创建用户,则需要更改请求模式。您必须像下面这样请求invitation API

请求网址: https://graph.microsoft.com/v1.0/invitations

请求正文:

{
  "invitedUserEmailAddress": "TestGmailUser@gmailUser",
  "inviteRedirectUrl": "https://myapp.com"
}

回复:

enter image description here

  

注意::如果转到azure portal,您将无法将gmail user添加为邀请后添加为来宾用户的域成员。所以这   为什么需要以上请求模式。希望你现在明白了。

Gmail用户添加SDK:

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var invitation = new Invitation
{
    InvitedUserEmailAddress = "TestGmailUser@gmailUser",
    InviteRedirectUrl = "https://myapp.com"
};

await graphClient.Invitations
    .Request()
    .AddAsync(invitation);

Azure Portal验证:

我已使用上述请求在门户网站上成功添加了gmail user。请参见以下屏幕截图:

enter image description here

答案 1 :(得分:0)

对于那些仍在努力实现这一目标的人来说,这里接受的答案没有帮助,或者至少在 2021 年不是这种情况。我能够使用个人电子邮件地址以及自定义登录名创建本地 B2C 帐户使用 Example 2: Create a user with social and local account identities

POST https://graph.microsoft.com/v1.0/users
Content-type: application/json

{
  "displayName": "John Smith",
  "identities": [
    {
      "signInType": "userName",
      "issuer": "contoso.onmicrosoft.com",
      "issuerAssignedId": "johnsmith"
    },
    {
      "signInType": "emailAddress",
      "issuer": "contoso.onmicrosoft.com",
      "issuerAssignedId": "jsmith@yahoo.com"
    },
    {
      "signInType": "federated",
      "issuer": "facebook.com",
     "issuerAssignedId": "5eecb0cd"
    }
  ],
  "passwordProfile" : {
     "password": "password-value",
    "forceChangePasswordNextSignIn": false
  },
  "passwordPolicies": "DisablePasswordExpiration"
}
相关问题