Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

.net - How do I authenticate my header token in C# API?

I'm trying to authenticate APIkeys through the header of my API rather than sending it as a query parameter in my controller. I've tried this method and I'm a bit lost of what to do right now. My APIkeys are currently stored in an Entity framework and the header should be able to authenticate them before letting the user do any CRUDs

using System.Threading.Tasks;
    using FygiEye.DB.Authentication;
    using FygiEye.Settings;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Options;
    
    namespace FygiEye.Middlewares
    
    {
    public class AuthentificatePublicAPIMiddleware
    
            {
                private const string AuthenticationHeaderKey = "auth-key";
                private readonly AuthenticationService authentificationService;
                private readonly RequestDelegate next;
    
                public AuthentificatePublicAPIMiddleware(RequestDelegate next, AuthenticationService authentificationService)
                {
                    this.next = next;
                    this.authentificationService = authentificationService;
                }
    
                public async Task Invoke(HttpContext httpContext)
                {
    //#if DEBUG
    //            await next.Invoke(httpContext);
    //            return;
    //#endif
                    // ReSharper disable once HeuristicUnreachableCode
    #pragma warning disable 162
                   
                    if (httpContext.Request.Headers.ContainsKey(AuthenticationHeaderKey))
                    {
                        if (httpContext.Request.Headers[AuthenticationHeaderKey].ToString() == AuthenticationHeaderKey)
                            
                        {
                            await authentificationService.Authenticate(AuthenticationHeaderKey);
                            await next.Invoke(httpContext);
                            return;
                        }
                    }
                    else if (httpContext.Request.Headers.ContainsKey("Authorization"))
                    {
                        if (httpContext.Request.Headers["Authorization"].ToString().Contains("Authorization"))
                        {
                           
                            await next.Invoke(httpContext);
                            return;
                        } 
                    }
    
                    httpContext.Response.StatusCode = 401;
                    await httpContext.Response.WriteAsync("Token is missing or invalid");
                    return;
    #pragma warning restore 162
                }
            }
    
            // Extension method used to add the middleware to the HTTP request pipeline.
            public static class AuthentificatePublicApiMiddlewareExtensions
            {
                public static IApplicationBuilder UseAuthentificatePublicAPIMiddleware(this IApplicationBuilder builder)
                {
                    return builder.UseMiddleware<AuthenticationService>();
                }
            }
        } 

 
question from:https://stackoverflow.com/questions/65951927/how-do-i-authenticate-my-header-token-in-c-sharp-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...