private static TokenRequestBody GetTokenRequestBody()
        {
            TokenRequestBody tokenRequestBody = new TokenRequestBody();

            tokenRequestBody.grant_type    = ConfigurationManager.AppSettings["GrantType"];
            tokenRequestBody.client_id     = ConfigurationManager.AppSettings["ClientId"];
            tokenRequestBody.client_secret = ConfigurationManager.AppSettings["ClientSecret"];
            tokenRequestBody.scope         = ConfigurationManager.AppSettings["Scope"];
            return(tokenRequestBody);
        }
        public async static Task <string> GetToken(HttpClient httpClient, TokenRequestBody tokenRequestBody)
        {
            string token = string.Empty;

            try
            {
                if (!httpClient.DefaultRequestHeaders.Contains("Accept"))
                {
                    httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
                }

                string grantType    = tokenRequestBody.grant_type;
                string clientId     = tokenRequestBody.client_id;
                string clientSecret = tokenRequestBody.client_secret;
                string scope        = tokenRequestBody.scope;
                string directoryId  = ConfigurationManager.AppSettings["DirectoryId"];

                var formContent = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("grant_type", grantType),
                    new KeyValuePair <string, string>("client_id", clientId),
                    new KeyValuePair <string, string>("client_secret", clientSecret),
                    new KeyValuePair <string, string>("scope", scope)
                });

                string path = $"https://login.microsoftonline.com/{directoryId}/oauth2/v2.0/token";

                HttpResponseMessage response = await httpClient.PostAsync(path, formContent);

                if (response.IsSuccessStatusCode)
                {
                    var tokenResponse = await response.Content.ReadAsAsync <TokenResponse>();

                    token = tokenResponse.access_token;
                }
                else
                {
                    token = string.Empty;
                }
            }
            catch (Exception ex)
            {
                token = string.Empty;
                Console.WriteLine("Error in computing token : " + ex.Message);
            }

            return(token);
        }
        public TokenResponseData GetToken(TokenRequestHeader tokenRequestHeader, TokenRequestBody tokenRequestBody)
        {
            var client = new RestClient(OpenAPI.Authentication.Token(_remoteServiceBaseUrl))
            {
                Timeout = -1
            };
            var request = new RestRequest(Method.POST);

            request.AddHeader("content-type", tokenRequestHeader.ContentType);
            request.AddHeader("resourceOwnerId", tokenRequestHeader.ResourceOwnerId);
            request.AddHeader("requestUId", tokenRequestHeader.RequestUId);
            request.AddHeader("accept-language", tokenRequestHeader.AcceptLanguage);
            request.AddJsonBody(JsonConvert.SerializeObject(tokenRequestBody));

            IRestResponse response           = client.Execute(request);
            var           responseContent    = JsonConvert.DeserializeObject <TokenResponseData>(response.Content);
            var           responseJsonString = JsonConvert.DeserializeObject(response.Content);
            var           results            = responseContent;

            results.jsonString = responseJsonString;
            return(results);
        }
Beispiel #4
0
 /// <summary>
 /// This endpoint generates the access token, which is used to access any consented resource of the user on SCB Developer platform.
 /// Required: TokenRequestHeader, TokenRequestBody
 /// </summary>
 /// <param name="tokenRequestHeader">Request Header</param>
 /// <param name="tokenRequestBody">Request Body</param>
 /// <returns>Access token</returns>
 public async Task <TokenResponseData> GetTokenAsync(TokenRequestHeader tokenRequestHeader, TokenRequestBody tokenRequestBody)
 {
     return(await _oauthService.GetTokenAsync(tokenRequestHeader, tokenRequestBody));
 }
Beispiel #5
0
 /// <summary>
 /// This endpoint generates the access token, which is used to access any consented resource of the user on SCB Developer platform.
 /// Required: TokenRequestHeader, TokenRequestBody
 /// </summary>
 /// <param name="tokenRequestHeader">Request Header</param>
 /// <param name="tokenRequestBody">Request Body</param>
 /// <returns>Access token</returns>
 public TokenResponseData GetToken(TokenRequestHeader tokenRequestHeader, TokenRequestBody tokenRequestBody)
 {
     return(_oauthService.GetToken(tokenRequestHeader, tokenRequestBody));
 }