Asp. Net Core sample for adding request header custom authentication

  • 2021-11-29 06:42:23
  • OfStack

Preface to the table of contents Key points The GuidToken class is our custom token manager. Finally, it is how to use it

Preface

Small projects need to add Api request authority authentication, and only use within the private network, so I just want to simply authenticate whether it can be accessed, by the way, it is also a learning process, simple record 1

Key points

Implementing IAuthenticationHandler interface: 4 methods

First, InitializeAsync is called to get scheme and context Then call AuthenticateAsync, get the verification information from Header in context, and then carry out relevant verification. According to different results, call ChallengeAsync or ForbidAsync respectively

public class HeaderAuth : IAuthenticationHandler {

       public AuthenticationScheme Scheme { get; private set; }

       public HttpContext CurrentContext { get; private set; }
       public Task<AuthenticateResult> AuthenticateAsync() {
           var token = CurrentContext.Request.Headers[GuidToken.GUID_TOKEN_KEY].ToString();

           var (isValid, tokenEntity) = GuidToken.Valid(token);

           if (!isValid || tokenEntity == null) {
               return Task.FromResult(AuthenticateResult.Fail(" You are not logged in or your authorization has expired. "));
           }
           //  Generate  AuthenticationTicket
           AuthenticationTicket ticket = new AuthenticationTicket(tokenEntity.ToClaimsPrincipal(), Scheme.Name);
           return Task.FromResult(AuthenticateResult.Success(ticket));
       }

       public Task ChallengeAsync(AuthenticationProperties properties) {
           CurrentContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
           return Task.CompletedTask;
       }

       public Task ForbidAsync(AuthenticationProperties properties) {
           CurrentContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
           return Task.CompletedTask;
       }

       public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context) {
           Scheme = scheme;
           CurrentContext = context;
           return Task.CompletedTask;
       }
   }

The GuidToken class is our custom token manager


public class GuidToken {
        public const string GUID_TOKEN_NAME = "MtGuidTokenAuthentication";
        public const string DEFAULT_AUTHENTICATION_TYPE = "local";
        public const int TOKEN_LENGTH = 32;
        public const string GUEST = "GUEST";
        public const string DEFAULT_ROLE = "USER";
        public const string DEFAULT_OPENID = "DEFAULT_OPENID";
        public const string GUID_TOKEN_KEY = "Token";
        private static int expireDuration = 0;
        public string OpenId { get; set; }
        public string Role { get; set; }
        public DateTime Expire { get; set; }

        private static readonly Dictionary<string, GuidToken> tokenCache = new Dictionary<string, GuidToken>();

        public static (bool, GuidToken) Valid(string token) {
            if (string.IsNullOrEmpty(token) || token.Length != TOKEN_LENGTH) {
                return (false, null);
            }

            //  From  Session  Get the token entity in 
            GuidToken tokenEntity = GetTokenCache();

            if (tokenEntity == null) {
                return (false, null);
            } else {
                tokenEntity.Expire = DateTime.Now.AddMinutes(expireDuration);
            }

            return (true, tokenEntity);

            GuidToken GetTokenCache() {
                if (tokenCache.TryGetValue(token, out var val)) {
                    if (val.Expire > DateTime.Now) return val;
                    else tokenCache.Remove(token);
                }
                return null;
            }

        }

        public static string Create(string openId = DEFAULT_OPENID, string role = DEFAULT_ROLE, int minutes = 30) {
            var token = Guid.NewGuid().ToString("N");
            expireDuration = minutes;
            var entity = new GuidToken {
                OpenId = openId,
                Role = role,
                Expire = DateTime.Now.AddMinutes(expireDuration)
            };
            tokenCache.Add(token, entity);
            return token;
        }

        /// <summary>
        ///  Token entity   Turn  ClaimsPrincipal
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public ClaimsPrincipal ToClaimsPrincipal() {
            var claimsIdentity = new ClaimsIdentity(new Claim[] {
                new Claim(ClaimTypes.Name, OpenId),
                new Claim(ClaimTypes.Role, Role),
            }, GuidToken.DEFAULT_AUTHENTICATION_TYPE);

            var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);

            return claimsPrincipal;
        }
    }

Finally, there is the way to use it

Configured in Startup


public void ConfigureServices(IServiceCollection services) {
    //  Register for use 
    services.AddAuthentication(options => {
        options.AddScheme<HeaderAuth>(GuidToken.GUID_TOKEN_NAME, "Default Guid Token");
        options.DefaultAuthenticateScheme = GuidToken.GUID_TOKEN_NAME;
        options.DefaultChallengeScheme = GuidToken.GUID_TOKEN_NAME;
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
    if (env.IsDevelopment()) {
        app.UseDeveloperExceptionPage();
    }
    app.UseCors("any");
    app.UseStaticFiles();
    //  Enable authentication 
    app.UseAuthentication();
    app.UseRouting();
    //  Enable authorization 
    app.UseAuthorization();
    app.UseEndpoints(endpoints => {
        endpoints.MapControllers();
    });
}

Using labels in controllers


[Authorize]
public class JobController : ControllerBase {}

The above is Asp. Net Core add request header custom authentication example details, more about Asp. Net Core add request header authentication information please pay attention to other related articles on this site!


Related articles: