예제 #1
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey("Authorization"))
            {
                return(AuthenticateResult.Fail("Missing Authorization Header"));
            }

            BrashApiAuthModel user = null;

            try
            {
                var authHeader      = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
                var credentials     = Encoding.UTF8.GetString(credentialBytes).Split(new[] { ':' }, 2);
                var apiAuthName     = credentials[0];
                var apiAuthPass     = credentials[1];

                user = await _apiAuthService.Authenticate(apiAuthName, apiAuthPass);
            }
            catch
            {
                return(AuthenticateResult.Fail("Invalid Authorization Header"));
            }

            if (user == null)
            {
                return(AuthenticateResult.Fail("Invalid AuthName or Password"));
            }

            var claims = new[] {
                new Claim(ClaimTypes.NameIdentifier, user.ApiAuthId.ToString()),
                new Claim(ClaimTypes.Name, user.ApiAuthName),
            };
            var identity  = new ClaimsIdentity(claims, Scheme.Name);
            var principal = new ClaimsPrincipal(identity);
            var ticket    = new AuthenticationTicket(principal, Scheme.Name);

            return(AuthenticateResult.Success(ticket));
        }
예제 #2
0
 public BrashApiAuthService AddAuthAccount(BrashApiAuthModel account)
 {
     _accounts.Add(account);
     return(this);
 }