/// <summary> /// This method sends an Azure guest user invitation to the provided email address. /// </summary> /// <param name="accessToken">The OAuth 2.0 Access Token to use for invoking the Microsoft Graph</param> /// <param name="guestUserEmail">Email of the user to whom the invite must be sent</param> /// <param name="redirectUri">URL where the user will be redirected after the invite is accepted.</param> /// <param name="customizedMessage">Customized email message to be sent in the invitation email.</param> /// <param name="guestUserDisplayName">Display name of the Guest user.</param> /// <returns></returns> public static Invitation InviteGuestUser(string accessToken, string guestUserEmail, string redirectUri, string customizedMessage = "", string guestUserDisplayName = "") { Invitation inviteUserResponse = null; try { Invitation invite = new Invitation(); invite.InvitedUserEmailAddress = guestUserEmail; if (!string.IsNullOrWhiteSpace(guestUserDisplayName)) { invite.InvitedUserDisplayName = guestUserDisplayName; } invite.InviteRedirectUrl = redirectUri; invite.SendInvitationMessage = true; // Form the invite email message body if (!string.IsNullOrWhiteSpace(customizedMessage)) { InvitedUserMessageInfo inviteMsgInfo = new InvitedUserMessageInfo(); inviteMsgInfo.CustomizedMessageBody = customizedMessage; invite.InvitedUserMessageInfo = inviteMsgInfo; } // Create the graph client and send the invitation. GraphServiceClient graphClient = CreateGraphClient(accessToken); inviteUserResponse = graphClient.Invitations.Request().AddAsync(invite).Result; } catch (ServiceException ex) { Log.Error(Constants.LOGGING_SOURCE, CoreResources.GraphExtensions_ErrorOccured, ex.Error.Message); throw; } return(inviteUserResponse); }
public async Task <string> PostUserAsync(string email, string name, string mensaje) { HttpClient client = new HttpClient(); var token = await AcquireAsync(); client.SetBearerToken(token); var invitation = $"https://graph.microsoft.com/v1.0/invitations"; var inv = new Invitation(); var invM = new InvitedUserMessageInfo(); invM.CustomizedMessageBody = mensaje; inv.InvitedUserDisplayName = name; inv.InvitedUserEmailAddress = email; inv.InvitedUserMessageInfo = invM; inv.InviteRedirectUrl = "https://dev-fichajes.azurewebsites.net"; inv.SendInvitationMessage = true; var cont = JsonConvert.SerializeObject(inv); var buffer = System.Text.Encoding.UTF8.GetBytes(cont); var c = new ByteArrayContent(buffer); c.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = await client.PostAsync(new Uri(invitation), c); if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); var dataComplete = JsonConvert.DeserializeXmlNode(content, "root"); var data = dataComplete.LastChild.LastChild.InnerText; return(data); } return(null); }