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

            Model.UserProfile        user  = null;
            Model.AdministratorLogin admin = null;
            try
            {
                var authHeader      = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
                var credentials     = Encoding.UTF8.GetString(credentialBytes).Split(':');
                var username        = credentials[0];
                var password        = credentials[1];
                user  = _userService.Authenticate(username, password);
                admin = _adminService.Authenticate(username, password);
            }
            catch
            {
                return(AuthenticateResult.Fail("Invalid Authorization Header"));
            }

            if (user == null && admin == null)
            {
                return(AuthenticateResult.Fail("Invalid Username or Password"));
            }
            if (admin != null)
            {
                var claims = new List <Claim> {
                    new Claim(ClaimTypes.NameIdentifier, admin.AdminName),
                    new Claim(ClaimTypes.Email, admin.AdminName + "@mdome.com"),
                };
                claims.Add(new Claim(ClaimTypes.Role, "Admin"));
                var identity  = new ClaimsIdentity(claims, Scheme.Name);
                var principal = new ClaimsPrincipal(identity);
                var ticket    = new AuthenticationTicket(principal, Scheme.Name);
                return(AuthenticateResult.Success(ticket));
            }
            else
            {
                var claims = new List <Claim> {
                    new Claim(ClaimTypes.NameIdentifier, user.Username),
                    new Claim(ClaimTypes.Email, user.Email),
                };
                claims.Add(new Claim(ClaimTypes.Role, "User"));
                var identity  = new ClaimsIdentity(claims, Scheme.Name);
                var principal = new ClaimsPrincipal(identity);
                var ticket    = new AuthenticationTicket(principal, Scheme.Name);
                return(AuthenticateResult.Success(ticket));
            }
        }