代码之家  ›  专栏  ›  技术社区  ›  Simple Code

添加Authorize属性时,Web api核心返回404

  •  0
  • Simple Code  · 技术社区  · 8 年前

    在启动类中,我是这样配置的:

    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.AddDbContext<MandarinDBContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("MyConnection")));
    
            services.AddIdentity<User, Role>()
            .AddEntityFrameworkStores<MyDBContext>()
            .AddDefaultTokenProviders();
    
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                    .AddJwtBearer(options =>
                    {
                        options.TokenValidationParameters = new TokenValidationParameters
                        {
                            ValidateIssuer = false,
                            ValidateAudience = false,
                            ValidateLifetime = true,
                            ValidateIssuerSigningKey = true,
                            ValidIssuer = "yourdomain.com",
                            ValidAudience = "yourdomain.com",
                            IssuerSigningKey = new SymmetricSecurityKey(
                                Encoding.UTF8.GetBytes("My secret goes here"))
                        };
    
                        options.RequireHttpsMetadata = false;
                    });
    
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    
            // Add application services.
            services.AddTransient<IUserService, UserService>();
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }
    
            app.UseHttpsRedirection();
    
            app.UseAuthentication();
            app.UseMvc();
        }
    }
    

    但当我尝试调用以下操作时:

        [Authorize]
        [HttpGet]
        [Route("api/Tokens")]
        public IActionResult TestAuthorization()
        {
            return Ok("You're Authorized");
        }
    

    1 回复  |  直到 8 年前
        1
  •  19
  •   Code Name Jack    8 年前

    当您的API未经授权并且您的重定向URL不存在时,就会发生这种情况。 当身份验证失败时,Web API将发送401代码。现在,如果您在客户端处理此代码并为授权失败执行重定向,请确保重定向的Url存在。 也, .

        [Authorize(AuthenticationSchemes = "Bearer")]
        [HttpGet]
        [Route("api/Tokens")]
        public IActionResult TestAuthorization()
        {
            return Ok("You're Authorized");
        }
    

    要使其成为默认身份验证方案,请将addentity更改为addentitycore。这是一篇很好的文章。

    Using JwtBearer Authentication in an API-only ASP.NET Core Project