代码之家  ›  专栏  ›  技术社区  ›  SamulP

具有API密钥和JWT令牌的Net Core API

  •  0
  • SamulP  · 技术社区  · 4 年前

    我有一个。Net-Core API,其中一些端点需要JWT授权,而其他端点需要API密钥授权。我为API Key方法实现了一个属性,同时在startup.cs中配置JWT身份验证后,对需要JWT令牌的方法使用Authorize。我走对路了吗?我是新手。Net Core和API,并感谢任何帮助。

    0 回复  |  直到 4 年前
        1
  •  0
  •   Hesam Akbari    4 年前

    是的,如你所知,这是正确的。启动类配置应用程序的请求管道以及如何处理所有请求

    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using WebApi.Helpers;
    using WebApi.Services;
    using Microsoft.IdentityModel.Tokens;
    using System.Text;
    using Microsoft.AspNetCore.Authentication.JwtBearer;
    
    namespace WebApi
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddCors();
                services.AddControllers();
    
                // configure strongly typed settings objects
                var appSettingsSection = Configuration.GetSection("AppSettings");
                services.Configure<AppSettings>(appSettingsSection);
    
                // configure jwt authentication
                var appSettings = appSettingsSection.Get<AppSettings>();
                var key = Encoding.ASCII.GetBytes(appSettings.Secret);
                services.AddAuthentication(x =>
                {
                    x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                    x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                })
                .AddJwtBearer(x =>
                {
                    x.RequireHttpsMetadata = false;
                    x.SaveToken = true;
                    x.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuerSigningKey = true,
                        IssuerSigningKey = new SymmetricSecurityKey(key),
                        ValidateIssuer = false,
                        ValidateAudience = false
                    };
                });
    
                // configure DI for application services
                services.AddScoped<IUserService, UserService>();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                app.UseRouting();
    
                // global cors policy
                app.UseCors(x => x
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader());
    
                app.UseAuthentication();
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints => {
                    endpoints.MapControllers();
                });
            }
        }
    }
    
    推荐文章