我有一个来自模板的简单 Blazor 应用程序。您可以在这里检查:https://blazorfun2.azurewebsites.net/ 。 FetchData 页面仅适用于授权用户,但是当我授权时,我得到 401 Unauthorized error
。在响应 header 下显示此错误:
WWW-Authenticate: Bearer error="invalid_token", error_description="The issuer 'https://blazorfun2.azurewebsites.net' is invalid"
您可以使用 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="097d6c7a7d64686065497d6c7a7d64686065277a66646c7d6160676e" rel="noreferrer noopener nofollow">[email protected]</a>
进行测试和Error#2
密码。
我在本地主机上没有此错误。
我正在 Linux 上使用 Azure 应用服务 dotnet 5.0。
可以通过改变 IssuerUri
来解决( source ),但我想这不是最好的选择..
//Main method in Program.cs in Client:
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.Services.AddHttpClient("BlazorPlaygroundFun.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
// Supply HttpClient instances that include access tokens when making requests to the server project
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("BlazorPlaygroundFun.ServerAPI"));
builder.Services.AddApiAuthorization();
await builder.Build().RunAsync();
//startup.cs in server project:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddControllersWithViews();
services.AddRazorPages();
这里出了什么问题?谢谢!
最佳答案
登录成功后,调用电话
var token = await TokenProvider.GetTokenAsync();
获取 jwt token 的方法。
然后在“获取数据”中,将 jwt token 带入 header ,这将解决您的问题。
更多详情,请查看相关帖子。
感谢enet的答案。
<强> Blazor WebAssembly 401 Unauthorized even when I am authorized
关于c# - Blazor WA 托管 - Azure AppService 上的 401 未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66199550/