微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

服务器端Razor页面的Blazor WebAssembly授权问题

如何解决服务器端Razor页面的Blazor WebAssembly授权问题

我在Visual Studio 2019中创建了Blazor WebAssembly应用程序,其中托管了选项ASP.NET Core,并在IdentityServer4上进行了身份验证(单个用户帐户,在应用程序中存储用户帐户)。从Visual Studio生成的程序。我没有进行任何修改。一切正常。我可以登录并查看具有[Authorize]属性的客户端页面

在下一步中,我将新的Razor页面添加到服务器端(不在客户端)-Reports.cshtml 启动程序后,我可以转到新创建的页面。很好。

现在,我要向服务器端Reports.cshtml页面添加[Authorize]属性。启动该程序并登录后,尝试显示Reports.cshtml页的尝试以错误结束:401未经授权

这是怎么了?

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UsesqlServer(
            Configuration.GetConnectionString("DefaultConnection")));

    services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
        .AddEntityFrameworkStores<ApplicationDbContext>();

    services.AddIdentityServer()
        .AddApiAuthorization<ApplicationUser,ApplicationDbContext>();

    services.AddAuthentication()
        .AddIdentityServerJwt();

    services.AddControllersWithViews();
    services.AddRazorPages();
}

public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
        app.UseWebAssemblyDebugging();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseBlazorFrameworkFiles();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseIdentityServer();
    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapControllers();
        endpoints.MapFallbackToFile("index.html");
    });
}

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-BlazorAuthTest1;Trusted_Connection=True;MultipleActiveResultSets=true"
  },"Logging": {
      "LogLevel": {
      "Default": "information","Microsoft": "Warning","Microsoft.Hosting.Lifetime": "information"
      }
    },"IdentityServer": {
    "Clients": {
      "BlazorAuth.Client": {
        "Profile": "IdentityServerSPA"
      }
    }
  },"AllowedHosts": "*"
}

谢谢您的帮助!

解决方法

我已经解决了我的问题,所以希望它能解决您的问题...

在您的startup.cs中,更改行

    services.AddAuthentication()
    .AddIdentityServerJwt();

收件人:

            services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
            options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
        })

            .AddIdentityServerJwt();

如果使用声明更改命名结构,则在API控制器中可能会有一些副作用,但这很容易解决,而且如果您习惯编写MVC,那么它会变得更像以前了。 。如果有道理。关于此的更多详细信息,我在其中发布了自己的问题的答案。链接到上面是我的评论。

最适合您的Hpe。让我知道。 干杯

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。