Ejemplo n.º 1
0
        public static async Task <GraphInvitation> SendInvitationAsync(GuestRequest request, string groupUrl, string groupId)
        {
            var displayName = $"{request.FirstName} {request.LastName}";
            var memberType  = GraphMemberType.Guest;

            GraphResponse <GraphInvitation> response = null;
            // Setup invitation
            var inviteEndPoint = $"{Constants.ResourceUrl}/{Constants.GraphApiBetaVersion}/invitations";
            var invitation     = new GraphInvitation()
            {
                InvitedUserDisplayName  = displayName,
                InvitedUserEmailAddress = request.EmailAddress,
                InviteRedirectUrl       = groupUrl,
                SendInvitationMessage   = false,
                InvitedUserType         = memberType.ToString()
            };

            // Invite user via Graph API
            response = await Client.GetData <GraphInvitation>(HttpMethod.Post, inviteEndPoint, invitation)
                       .ConfigureAwait(false);

            if (!response.IsSuccessfull)
            {
                throw new Exception($"Error: invite not sent - API error: {response.Message}");
            }

            // Add user to group
            var groupAdded = await AddUserToGroupAsync(response.Data.InvitedUser.Id,
                                                       groupId).ConfigureAwait(false);

            if (!groupAdded)
            {
                throw new Exception($"Error: could not add user {response.Data.InvitedUserEmailAddress} to group {groupId}");
            }

            // Send email to user
            if (!await Mailer.SendInvitationEmailAsync(request.EmailAddress, response.Data.InviteRedeemUrl))
            {
                throw new Exception($"Error: invite not send to {request.EmailAddress}.");
            }

            return(response.Data);
        }
Ejemplo n.º 2
0
        public static async Task <GraphUser> GetUserByMailAddress(string mailAddress)
        {
            var uriEndPoint =
                $"{Constants.ResourceUrl}/{Constants.GraphApiVersion}/users?$filter=mail eq '{mailAddress}' and userType eq 'Guest'";

            // Retrieve possible users from Graph API
            var users = await Client.GetData <GraphFilteredUsers>(HttpMethod.Get, Uri.EscapeUriString(uriEndPoint))
                        .ConfigureAwait(false);

            // If request failed return nothing and log error
            if (!users.IsSuccessfull)
            {
                throw new Exception($"Error: unable to retrieve user. API error {users.Message}");
            }

            // Check if there is any user returned by the Graph API
            if (users.Data.Users.Any())
            {
                return(users.Data.Users.First());
            }

            return(null);
        }
Ejemplo n.º 3
0
        public static async Task <GraphGroup> GetGroupByAccountIdAsync(string accountId)
        {
            var uriEndPoint =
                $"{Constants.ResourceUrl}/{Constants.GraphApiVersion}/groups?$filter=startswith(mailNickname, '{accountId}')";

            // Retrieve possible groups from Graph API
            var groups = await Client.GetData <GraphFilteredGroups>(HttpMethod.Get, Uri.EscapeUriString(uriEndPoint))
                         .ConfigureAwait(false);

            // If request failed return nothing and log error
            if (!groups.IsSuccessfull)
            {
                throw new Exception($"Error: unable to retrieve group. API error {groups.Message}");
            }

            // Check if there is any group returned by the Graph API
            if (groups.Data.Groups.Any())
            {
                return(groups.Data.Groups.First());
            }

            return(null);
        }