示例#1
0
        /// <summary>
        ///     Acquires a token interactively Acquire a token with possible ui prompts
        /// </summary>
        /// <param name="body">AuthParameters</param>
        /// <returns>MsalAuthenticationResult</returns>
        public MsalAuthenticationResult AcquireTokenInteractive(MsalAuthParameters body)
        {
            // verify the required parameter 'body' is set
            if (body == null)
            {
                throw new ApiException(400, "Missing required parameter 'body' when calling AcquireTokenInteractive");
            }

            string path = "/pca/acquireTokenInteractive";

            path = path.Replace("{format}", "json");

            var    queryParams  = new Dictionary <string, string>();
            var    headerParams = new Dictionary <string, string>();
            var    formParams   = new Dictionary <string, string>();
            var    fileParams   = new Dictionary <string, FileParameter>();
            string postBody     = null;

            postBody = ApiClient.Serialize(body); // http body (model) parameter

            // authentication setting, if any
            var authSettings = new string[]
            {
                "msal_auth"
            };

            // make the HTTP request
            var response = (IRestResponse)ApiClient.CallApi(
                path,
                Method.POST,
                queryParams,
                postBody,
                headerParams,
                formParams,
                fileParams,
                authSettings);

            if ((int)response.StatusCode >= 400)
            {
                throw new ApiException(
                          (int)response.StatusCode,
                          "Error calling AcquireTokenInteractive: " + response.Content,
                          response.Content);
            }
            else if ((int)response.StatusCode == 0)
            {
                throw new ApiException(
                          (int)response.StatusCode,
                          "Error calling AcquireTokenInteractive: " + response.ErrorMessage,
                          response.ErrorMessage);
            }

            return((MsalAuthenticationResult)ApiClient.Deserialize(
                       response.Content,
                       typeof(MsalAuthenticationResult),
                       response.Headers));
        }
        public virtual async Task <IActionResult> AcquireTokenInteractive([FromBody] MsalAuthParameters body)
        {
            string pcaId = body.ClientApplicationId;

            if (string.IsNullOrWhiteSpace(pcaId))
            {
                return(BadRequest());
            }

            if (!_pcaMap.TryGetValue(pcaId, out var pca))
            {
                return(NotFound());
            }

            try
            {
                var authResult = await pca.AcquireTokenByIntegratedWindowsAuthAsync(body.RequestedScopes.Split(" ").ToList(), body.Username);

                return(new ObjectResult(
                           new MsalAuthenticationResult
                {
                    AccessToken = authResult.AccessToken,
                    ExpiresOn = authResult.ExpiresOn.UtcDateTime,
                    IdToken = authResult.IdToken,
                    Scopes = string.Join(' ', authResult.Scopes)
                }));
            }
            catch (Exception ex) when(ex is MsalServiceException || ex is MsalClientException)
            {
                return(new ObjectResult(
                           new MsalAuthenticationResult
                {
                    IsError = true,
                    AccessToken = ex.Message
                }));
            }

            ////TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            //// return StatusCode(200, default(MsalAuthenticationResult));

            ////TODO: Uncomment the next line to return response 400 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            //// return StatusCode(400);

            //string exampleJson = null;
            //exampleJson = "{\n  \"isCanceled\" : false,\n  \"isError\" : false,\n  \"idToken\" : \"idToken\",\n  \"errorCode\" : 0,\n  \"expiresOn\" : \"2000-01-23T04:56:07.000+00:00\",\n  \"scopes\" : \"scopes\",\n  \"accessToken\" : \"accessToken\"\n}";

            //var example = exampleJson != null
            //? JsonConvert.DeserializeObject<MsalAuthenticationResult>(exampleJson)
            //: default(MsalAuthenticationResult);
            ////TODO: Change the data returned
            //return new ObjectResult(example);
        }
        public async Task <MsalAuthenticationResult> AcquireTokenByInteractiveWindowsAuthAsync(string redirectUri, IEnumerable <string> requestedScopes, string username)
        {
            var authParams = new MsalAuthParameters
            {
                ClientApplicationId = _publicClientApiId,
                AuthorizationType   = "Interactive",
                RedirectUri         = redirectUri,
                RequestedScopes     = string.Join(" ", requestedScopes),
                Username            = username
            };

            return(await Task.Run(() => _publicClientApi.AcquireTokenInteractive(authParams)));
        }
        public virtual IActionResult AcquireTokenSilent([FromBody] MsalAuthParameters body)
        {
            //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            // return StatusCode(200, default(MsalAuthenticationResult));

            //TODO: Uncomment the next line to return response 400 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
            // return StatusCode(400);

            string exampleJson = null;

            exampleJson =
                "{\n  \"isCanceled\" : false,\n  \"isError\" : false,\n  \"idToken\" : \"idToken\",\n  \"errorCode\" : 0,\n  \"expiresOn\" : \"2000-01-23T04:56:07.000+00:00\",\n  \"scopes\" : \"scopes\",\n  \"accessToken\" : \"accessToken\"\n}";

            var example = exampleJson != null
                              ? JsonConvert.DeserializeObject <MsalAuthenticationResult>(exampleJson)
                              : default(MsalAuthenticationResult);

            //TODO: Change the data returned
            return(new ObjectResult(example));
        }