亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ASP.NET Core 托管 Blazor 模板中的授權問題

ASP.NET Core 托管 Blazor 模板中的授權問題

C#
大話西游666 2023-08-27 16:02:17
我在 api 的控制器函數上使用[Authorize]屬性,它總是顯示數據,這些屬性不起作用,當我調試身份用戶時,我發現它沒有經過身份驗證,但它總是發送 json 數據,而它應該發送未經身份驗證的響應,任何幫助知道為什么授權屬性不起作用嗎?using System;using System.IO.Compression;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.ResponseCompression;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Hosting;using Newtonsoft.Json.Serialization;using System.Linq;using System.Security.Cryptography.X509Certificates;using MedicalDivision.Server.Security;using Microsoft.Extensions.Configuration;using Microsoft.AspNetCore.Authentication.JwtBearer;using Microsoft.AspNetCore.Authorization;using Microsoft.AspNetCore.Http;using Microsoft.Extensions.DependencyInjection.Extensions;namespace test.Server{    public class Startup    {        private X509Certificate2 Cert { get; }        private IConfiguration Configuration { get; }        private IWebHostEnvironment Env { get; }        private ITokenProvider TokenProvider { get; }        private PasswordHelper PasswordHelper { get; }        private IHttpContextAccessor httpContextAccessor { get; }        private IServiceProvider ServiceProvider { get; }        public readonly string _myAllowSpecificOrigins = "_myAllowSpecificOrigins";        public Startup(IConfiguration configuration,IWebHostEnvironment env,IServiceProvider serviceProvider)        {            ServiceProvider = serviceProvider;            Configuration = configuration;            Env = env;            Cert = new X509Certificate2(Convert.FromBase64String(Configuration["Auth:Cert:Data"]),  Configuration["Auth:Cert:Password"], X509KeyStorageFlags.MachineKeySet);            TokenProvider =new JwtTokenProvider(Cert, Configuration,env);            PasswordHelper = new PasswordHelper();            httpContextAccessor = ServiceProvider.GetService<IHttpContextAccessor>();        }
查看完整描述

2 回答

?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

后面需要配置aut&auth的中間件:


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

  // not here 

  //   app.UseAuthentication();

  //  app.UseAuthorization(); 

    app.UseResponseCompression();


    if (env.IsDevelopment())

    {

        app.UseDeveloperExceptionPage();

        app.UseBlazorDebugging();

    }


    app.UseCors(_myAllowSpecificOrigins);

    app.UseStaticFiles();

    app.UseClientSideBlazorFiles<Client.Startup>();


    app.UseRouting();


  // but here 

    app.UseAuthentication();

    app.UseAuthorization(); 


    app.UseEndpoints(endpoints =>

    {

        endpoints.MapDefaultControllerRoute();

        endpoints.MapFallbackToClientSideBlazor<Client.Startup>("index.html");

    });

}

它們必須添加到路由下方(在路由之后執行)。


查看完整回答
反對 回復 2023-08-27
?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

也許你需要這樣的東西:


services.AddMvcCore(options =>

{

    var policy = new AuthorizationPolicyBuilder()

        .RequireAuthenticatedUser()

        .Build();

    options.Filters.Add(new AuthorizeFilter(policy));

});


查看完整回答
反對 回復 2023-08-27
  • 2 回答
  • 0 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號