public static void InitializeTokens(RestServiceConsumptionOptions options, string serviceName) { var sessionOAuth2TokenData = OAuth2SessionData <OAuth2TokenData> .Get(serviceName); switch (options.oAuth2GrantType) { case OAuth2GrantType.Password: if (sessionOAuth2TokenData.Refresh_token == null) { OAuth2SessionData <OAuth2TokenData> .Initialize(serviceName); } else { sessionOAuth2TokenData.ForceRefreshToken = true; OAuth2SessionData <OAuth2TokenData> .Set(serviceName, sessionOAuth2TokenData); } break; case OAuth2GrantType.WebServer: if (sessionOAuth2TokenData.Refresh_token == null) { OAuth2SessionData <OAuth2TokenData> .Initialize(serviceName); OAuth2SessionData <OAuth2Code> .Initialize(serviceName); } else { sessionOAuth2TokenData.ForceRefreshToken = true; OAuth2SessionData <OAuth2TokenData> .Set(serviceName, sessionOAuth2TokenData); } break; } }
public static OAuth2TokenData GetSessionToken(string serviceName, RestServiceConsumptionOptions options, HttpContext httpContext) { try { //if (serviceName == null) //{ // return null; //} var sessionOAuth2TokenData = OAuth2SessionData <OAuth2TokenData> .Get(serviceName); if (sessionOAuth2TokenData != null) { if (!sessionOAuth2TokenData.ForceRefreshToken) { return(sessionOAuth2TokenData); } } var res = 0; Dictionary <string, string> dictParams = null; var refreshTokenWasUsed = false; if (sessionOAuth2TokenData == null) { sessionOAuth2TokenData = new OAuth2TokenData(); if (options.oAuth2GrantType == OAuth2GrantType.WebServer) { var sessionAuth2Code = OAuth2SessionData <OAuth2Code> .Get(serviceName)?.Code; if (sessionAuth2Code == null) { OAuth2SessionData <OAuth2ReturnUrl> .Set(serviceName, new OAuth2ReturnUrl(zAppDev.DotNet.Framework.Utilities.Web.GetRequestUri().ToString())); var simpleTask = Task.Run(() => { res = GetWebServerAuthorization(options, httpContext); }); simpleTask.Wait(); return(null); } dictParams = new Dictionary <string, string> { { "grant_type", "authorization_code" }, { "code", sessionAuth2Code }, { "client_id", options.ClientId }, { "client_secret", options.ClientSecret }, //{"scope", ""}, { "redirect_uri", options.CallBackUrl } }; } else if (options.oAuth2GrantType == OAuth2GrantType.Password) { dictParams = new Dictionary <string, string> { { "grant_type", "password" }, { "client_id", options.ClientId }, { "client_secret", options.ClientSecret }, { "username", options.UserName }, { "password", options.Password } }; } } else { sessionOAuth2TokenData.ForceRefreshToken = false; OAuth2SessionData <OAuth2TokenData> .Set(serviceName, sessionOAuth2TokenData); refreshTokenWasUsed = true; dictParams = new Dictionary <string, string> { { "grant_type", "refresh_token" }, { "refresh_token", sessionOAuth2TokenData.Refresh_token }, { "client_id", options.ClientId }, { "client_secret", options.ClientSecret } }; } var task = Task.Run(async() => { res = await GetOAuth2Token.GetAuthToken( options.AccessTokenUrl, dictParams, sessionOAuth2TokenData); }); task.Wait(); if (res != 0) { if (refreshTokenWasUsed) { OAuth2SessionData <OAuth2TokenData> .Initialize(serviceName); OAuth2SessionData <OAuth2Code> .Initialize(serviceName); } return(null); } OAuth2SessionData <OAuth2TokenData> .Set(serviceName, sessionOAuth2TokenData); return(sessionOAuth2TokenData); } catch (Exception) { return(null); } }