We are using ADAL.Net to create users in Azure AD. using SinginNames
, we can provide any email address (gmail or non-domain emails) as username to create Azure AD Local account.
when we try the same using Microsoft Graph (MSAL.Net), we are not able to create a user:
Code: Request_BadRequest
Message: Property userPrincipalName is invalid.
How can we create gmail address as the username using Microsoft Graph or the Microsoft Graph Client Library?
The newly created account should be local user account, not a guest user.
var user = new User
{
AccountEnabled = true,
DisplayName = "displayName-value",
MailNickname = "mailNickname-value",
UserPrincipalName = "vetrivelmp1@gmail.com",
PasswordProfile = new PasswordProfile
{
ForceChangePasswordNextSignIn = true,
Password = "Test123!@#"
}
};
var graphClient = await _msGraphHelper.GetMsGraphClientAsync();
var createdUser = await graphClient
.Users
.Request()
.AddAsync(user);
No You can not. You must have to request with tenant specific email
following sample:
Request URL: 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"
}
}
Points to Remember:
userPrincipalName
should follow as
UserName@tenant-value.onmicrosoft.com
"userType":"guest" Or "Member" You can add
Note: Mail should be like
myUser@Mytenant.onmicrosoft.com
Other then you would encounter 400 request error likeProperty userPrincipalName is invalid
Your Case:
If you wanted to create user using gmail account
then request pattern need to be changed You have to request for invitation API like below:
Request Url: https://graph.microsoft.com/v1.0/invitations
Request Body:
{
"invitedUserEmailAddress": "TestGmailUser@gmailUser",
"inviteRedirectUrl": "https://myapp.com"
}
Response:
Note: If go to
azure portal
you would see you cannot addgmail user
as domain member you add as guest user after invitation. So this why you need above request pattern. Hope you are clear now.
Gmail User Add 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 Verify:
I have successfully added gmail user
on my portal using above request. See the screen shot below: