Esempio n. 1
0
        public AuthCodeRequestData RequestConsentCode(AuthCodeRequest codeRequest)
        {
            var state  = Guid.NewGuid().ToString("N");
            var reqUrl = MsGraph.GetConsentUrl(codeRequest.Upn, codeRequest.Registration.ClientId,
                                               state, codeRequest.CallbackUrl.UrlEncode());

            return(new AuthCodeRequestData
            {
                AuthCodeRequestUrl = reqUrl,
                State = state
            });
        }
Esempio n. 2
0
        public string GetLogoutUrl(string tenantId, string clientId, string redirectUrl)
        {
            if (string.IsNullOrWhiteSpace(clientId))
            {
                return(null);
            }

            // See https://msdn.microsoft.com/en-us/office/office365/howto/authentication-v2-protocols
//            var request = MsGraph.LogoutUrl + "?post_logout_redirect_uri={0}"
//                              .Fmt(redirectUrl);
            // return authService.Redirect(LogoutUrlFilter(this, request));
            return(MsGraph.GetLogoutUrl(tenantId, clientId, redirectUrl));
        }
Esempio n. 3
0
        public AzureUserObject[] UsersByGroup(string authToken, string groupName)
        {
            var grp = GetGroupByName(authToken, groupName);

            if (grp == null)
            {
                return(new AzureUserObject[0]);
            }

            //var data = MsGraph.GetMembersByGroupUrl(grp.Id);
            var usrs = ExecuteGet <AzureUserObject[]>(authToken, MsGraph.GetMembersByGroupUrl(grp.Id)); // GraphResponse<AzureUserObject[]>.Parse(data);

            return(usrs.Value);
        }
Esempio n. 4
0
        public TokenResponse RequestAuthToken(AuthTokenRequest tokenRequest)
        {
            if (tokenRequest == null)
            {
                throw new ArgumentNullException(nameof(tokenRequest));
            }

            if (tokenRequest.Registration == null)
            {
                throw new ArgumentException("No directory registration specified.", nameof(tokenRequest.Registration));
            }

            if (string.IsNullOrWhiteSpace(tokenRequest.CallbackUrl))
            {
                throw new ArgumentException("No callback url specified.", nameof(tokenRequest.CallbackUrl));
            }

            if (string.IsNullOrWhiteSpace(tokenRequest.RequestCode))
            {
                throw new ArgumentException("No requests code specified", nameof(tokenRequest.RequestCode));
            }

            if (tokenRequest?.Scopes.Any() == false)
            {
                throw new ArgumentException("No scopes provided", nameof(tokenRequest.Scopes));
            }

            var postData =
                $"grant_type=authorization_code&redirect_uri={tokenRequest.CallbackUrl.UrlEncode()}&code={tokenRequest.RequestCode}&client_id={tokenRequest.Registration.ClientId}&client_secret={tokenRequest.Registration.ClientSecret.UrlEncode()}&scope={BuildScopesFragment(tokenRequest.Scopes)}";
            var result = MsGraph.TokenUrl.PostToUrl(postData);

            var authInfo    = JsonObject.Parse(result);
            var authInfoNvc = authInfo.ToNameValueCollection();

            if (MsGraph.RespondedWithError(authInfoNvc))
            {
                throw new AzureServiceException(MsGraph.TokenUrl, authInfoNvc);
            }

            return(new TokenResponse
            {
                AuthData = authInfoNvc,
                AccessToken = authInfo["access_token"],
                RefreshToken = authInfo["refresh_token"],
                IdToken = authInfo["id_token"],
                TokenExpirationSeconds = authInfo["expires_in"]
            });
        }
Esempio n. 5
0
        public async Task <AzureUserObject[]> UsersByGroupAsync(string authToken, string groupName)
        {
            var grp = await GetGroupByNameAsync(authToken, groupName);

            if (grp == null)
            {
                return(new AzureUserObject[0]);
            }

            var data = await MsGraph.GetMembersByGroupUrl(grp.Id).GetStringFromUrlAsync(
                requestFilter: req => { req.AddBearerToken(authToken); });

            var usrs = GraphResponse <AzureUserObject[]> .Parse(data);

            return(usrs.Value);
        }
Esempio n. 6
0
        public async Task <AzureGroupObject> GetGroupByNameAsync(string authToken, string groupName)
        {
            var grp = await ExecuteGetAsync <AzureGroupObject[]>(authToken, MsGraph.GetGroupObjectByNameUrl(groupName));

            return((grp.Value == null || grp.Value.Length == 0) ? null : grp.Value[0]);
        }
Esempio n. 7
0
        public AzureGroupObject GetGroupByName(string authToken, string groupName)
        {
            var grp = ExecuteGet <AzureGroupObject[]>(authToken, MsGraph.GetGroupObjectByNameUrl(groupName));

            return((grp.Value == null || grp.Value.Length == 0) ? null : grp.Value[0]);
        }