public ActionResult <TokenResponse> GenerateToken([FromHeader(Name = "Authorization")] string authentication, [FromForm] List <KeyValuePair <string, string> > content)
        {
            if (!authentication.StartsWith("Basic "))
            {
                return(NotFound());
            }

            var authorization = BasicAuthorization.Parse(authentication);

            if (authorization == null)
            {
                return(NotFound());
            }

            var clientInfoFromDb = _clientInfoRepository.GetRecord(info =>
                                                                   info.ClientId == authorization.ClientId && info.ClientSecret == authorization.ClientSecret);

            if (clientInfoFromDb == null)
            {
                return(NotFound());
            }

            var token = _tokenRepository.InsertRecord(new Token {
                AccessToken = Guid.NewGuid().ToString()
            });

            return(Ok(new TokenResponse {
                AccessToken = token.AccessToken
            }));
        }
Esempio n. 2
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey("Authorization"))
            {
                return(AuthenticateResult.Fail("Authorization header is required"));
            }

            try
            {
                var      authenticationHeaderValue = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                var      bytes       = Convert.FromBase64String(authenticationHeaderValue.Parameter);
                string[] credentials = Encoding.UTF8.GetString(bytes).Split(":");

                string userName = credentials[0];
                string password = credentials[1];

                BasicAuthorization basicAuthorization = _context.BasicAuthorization.Where(user => user.UserName == userName && user.Password == password).FirstOrDefault();

                if (basicAuthorization == null)
                {
                    return(AuthenticateResult.Fail("Invalid username or password"));
                }
                else
                {
                    var claims    = new[] { new Claim(ClaimTypes.Name, basicAuthorization.UserName) };
                    var identity  = new ClaimsIdentity(claims, Scheme.Name);
                    var principal = new ClaimsPrincipal(identity);
                    var ticket    = new AuthenticationTicket(principal, Scheme.Name);

                    return(AuthenticateResult.Success(ticket));
                }
            }
            catch (Exception)
            {
                return(AuthenticateResult.Fail("An exception has occured!"));
            }
        }
 public HttpTransport(string username, string password)
 {
     _paginationErrorCode = Properties.Resources.PaginationExceptionCode;
     _authorizationToken  = BasicAuthorization.GetToken(username, password);
 }