예제 #1
0
        public RedirectResult GenerateToken()
        {
            var scopes  = "";
            var authUrl = _oAuth2Service.GenerateAuthUrl(scopes);

            return(new RedirectResult(authUrl));
        }
예제 #2
0
        private async Task <OAuthAuthorization> AuthorizeAsync()
        {
            var contextKey = $"{nameof(DigikeyApi)}-{_httpContextAccessor.HttpContext.User.Identity.Name}";
            // check if we have an in-memory auth credential
            var getAuth = ServerContext.Get <OAuthAuthorization>(contextKey);

            if (getAuth != null && getAuth.IsAuthorized)
            {
                return(getAuth);
            }

            // check if we have a saved to disk auth credential
            OAuthAuthorization authRequest;
            var credential = await _credentialService.GetOAuthCredentialAsync(contextKey);

            if (credential != null)
            {
                // reuse a saved oAuth credential
                authRequest = new OAuthAuthorization(nameof(DigikeyApi), _oAuth2Service.ClientSettings.ClientId, _httpContextAccessor.HttpContext.Request.Headers["Referer"].ToString())
                {
                    AccessToken           = credential.AccessToken,
                    RefreshToken          = credential.RefreshToken,
                    CreatedUtc            = credential.DateCreatedUtc,
                    ExpiresUtc            = credential.DateExpiresUtc,
                    AuthorizationReceived = true,
                };
                // also store it in memory
                ServerContext.Set(contextKey, authRequest);
                return(authRequest);
            }

            // user must authorize
            // request a token if we don't already have one
            var scopes  = "";
            var authUrl = _oAuth2Service.GenerateAuthUrl(scopes);

            // OpenBrowser(authUrl);
            authRequest = new OAuthAuthorization(nameof(DigikeyApi), _oAuth2Service.ClientSettings.ClientId, _httpContextAccessor.HttpContext.Request.Headers["Referer"].ToString());
            ServerContext.Set(contextKey, authRequest);

            return(new OAuthAuthorization(nameof(DigikeyApi), true, authUrl));
        }