Example #1
0
        public IActionResult GetAuthorizationExchangeCode(string clientId, string scopes)
        {
            if (Request.Cookies.ContainsKey(OAUTH2_COOKIE))
            {
                var tokenString = Request.Cookies[OAUTH2_COOKIE];
                var token       = _context.Credential.Find(tokenString);
                if (token != null && token.IsValid())
                {
                    var client = _context.RegisteredClient.Find(clientId);
                    if (client == null)
                    {
                        return(NotFound());
                    }

                    var exchange = new ExchangeToken()
                    {
                        ExchangeCode = Guid.NewGuid().ToString(),
                        Credential   = Credential.GenerateCredential(token.AccountId, scopes)
                    };
                    _memoryCache.Set(exchange.ExchangeCode, exchange,
                                     new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(180)));
                    return(Redirect(client.RedirectUrl + "?code=" + exchange.ExchangeCode));
                }
            }
            return(RedirectToAction("Authentication", new { redirectUrl = Request.GetDisplayUrl() }));
        }
Example #2
0
        public IActionResult Authentication(LoginInformation loginInformation)
        {
            if (!ModelState.IsValid)
            {
                return(View("Login", loginInformation));
            }

            Account existAccount = _context.Account.FirstOrDefault(m => m.Email == loginInformation.Email);

            if (existAccount == null)
            {
                return(View("Login", loginInformation));
            }

            if (PasswordHandle.PasswordHandle.GetInstance().EncryptPassword(loginInformation.Password, existAccount.Salt) != existAccount.Password)
            {
                return(View("Login", loginInformation));
            }

            Credential credential = Credential.GenerateCredential(existAccount.Id, new List <CredentialScope>()
            {
                CredentialScope.Basic
            });

            _context.Credential.Add(credential);
            _context.SaveChanges();
            Response.Cookies.Append(
                OAUTH2_COOKIE,
                credential.AccessToken,
                new CookieOptions()
            {
                Path = "/"
            }
                );
            return(Redirect(loginInformation.RedirectUrl));
        }