Simple token based authentication/authorization in asp.net core for Mongodb datastore

前端 未结 2 441
南笙
南笙 2020-12-01 17:20

I need to implement pretty simple auth mechanizm with basically 2 roles: Owners and Users. And I think that having Enum for that will be enough. Ap

2条回答
  •  借酒劲吻你
    2020-12-01 17:43

    Let me clarify a little @Adem's answer. You need to to implement custom middleware in specific way. There is 3 abstract classes that need to be implemented to implementing this (answer is correct for asp.net core rc2btw):

    Microsoft.AspNetCore.Builder.AuthenticationOptions Microsoft.AspNetCore.Authentication.AuthenticationMiddleware Microsoft.AspNetCore.Authentication.AuthenticationHandler

    and then add this middleware to your startup class.

    Code example:

    public class TokenOptions : AuthenticationOptions
        {
            public TokenOptions() : base()
            {
                AuthenticationScheme = "Bearer";
                AutomaticAuthenticate = true;
            }
        }
    
    public class AuthMiddleware : AuthenticationMiddleware
    {
        protected override AuthenticationHandler CreateHandler()
        {
           return new AuthHandler(new TokenService());
        }
    
        public AuthMiddleware(RequestDelegate next, IOptions options, ILoggerFactory loggerFactory, UrlEncoder encoder) : base(next, options, loggerFactory, encoder)
        {
        }
    }
    
    public class AuthHandler : AuthenticationHandler
    {
        private ITokenService _tokenService;
    
        public AuthHandler(ITokenService tokenService)
        {
            _tokenService = tokenService;
        }
    
        protected override async Task HandleAuthenticateAsync()
        {
            string token = null;
            AuthenticateResult result = null;
            string token = Helper.GetTokenFromHEader(Request.Headers["Authorization"]);
            // If no token found, no further work possible
            if (string.IsNullOrEmpty(token))
            {
                result = AuthenticateResult.Skip();
            }
            else
            {
                bool isValid = await _tokenService.IsValidAsync(token);
                if (isValid)
                {
                    //assigning fake identity, just for illustration
                    ClaimsIdentity claimsIdentity = new ClaimsIdentity("Custom");
                    var claims = new List();
                    claims.Add(new Claim(ClaimTypes.Name, "admin"));
                    claims.Add(new Claim(ClaimTypes.NameIdentifier, "admin"));
                    claims.Add(new Claim(ClaimTypes.Role, "admin"));
                    ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
                    result =
                        AuthenticateResult.Success(new AuthenticationTicket(claimsPrincipal,
                            new AuthenticationProperties(), Options.AuthenticationScheme));
                }
                else
                {
                    result = AuthenticateResult.Skip();
                }
            }
    
            return result;
        }
    }`
    

    p.s. The code is just for illustration of idea. You will need to implement your own handler of course.

提交回复
热议问题