用户名 属性 在 Blazor Server AuthenticationStateProvider 中为空?
User name property is null in Blazor Server AuthenticationStateProvider?
我在 Blazor 服务器端工作,我想访问 windows 用户名。根据文档,我尝试使用 AuthenticationStateProvider
对象执行此操作。问题是当我 运行 解决方案处于发布模式时,用户名 属性 为空。在 Express 模式下它工作正常,我可以获得用户名 属性,在发布模式下该值为空。
这是 AuthenticationStateProvider 对象:
public class Authenticator
{
public readonly AuthenticationStateProvider _auth;
public Authenticator(AuthenticationStateProvider authenticationStateProvider)
{
_auth = authenticationStateProvider;
}
}
我在Startup.cs
中注入它:
services.AddScoped<Authenticator>();
我启用了windows身份验证:
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:49349",
"sslPort": 0
}
}
我是这样使用对象的:
return _auth.GetAuthenticationStateAsync().Result.User.Identity.Name.Substring(3);
我还启用了 WebSocketProtocol:
User对象的Name属性为null,仅在release模式下,在IIS Express的正常Debug模式下,正常运行。
我使用 ASP.NET CORE 3.1 和 VisualStudio 16.9.4。在 ISS 上发布应用程序后,我在 Windows Server 2012 上也遇到了这个问题。
编辑 1:在将 IHttpContextAccessor 与 Blazor-Server 一起使用时,我也遇到了同样的问题。它在 IIS Express 中获取用户名,但在发布模式下,名称为空。
编辑 2:当我 运行 使用应用程序名称而不是 IIS Express 的应用程序时出现问题。从我在网上看到的,这是在背后使用Kestrel服务器。
// Add Identity Order = 1 !important
services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
options.Password.RequiredLength = ConfigIdentity.Password_RequiredLength;
options.Password.RequireLowercase = ConfigIdentity.Password_RequireLowercase;
options.Password.RequireUppercase = ConfigIdentity.Password_RequireUppercase;
options.Password.RequireNonAlphanumeric = ConfigIdentity.Password_RequireNonAlphanumeric;
options.Password.RequireDigit = ConfigIdentity.Password_RequireDigit;
}).AddUserManager<UsersManager>().AddRoleManager<RolesManager>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
// Database Contexts Order = 2 !important
services.AddDbContext<ApplicationDbContext>(ServiceLifetime.Transient);
// 3 : Cookie Options
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = "/Error/AccessDenied";
options.Cookie.Name = "MyAPP";
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.LoginPath = "/";
//options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
options.SlidingExpiration = true;
});
services.AddHttpContextAccessor();
services.AddRazorPages();
services.AddControllers();
services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
如果其他人会遇到这个问题,我设法通过安装 ASP.NET Core 5.0 Runtime (v5.0.6) - Windows Hosting Bundle 和 URL Rewrite Module 使其工作.我还向 NETWORK 和 IIS_IUSRS 用户添加了对发布文件夹的完全权限,并修改了 IIS 网络应用程序授权并禁用了除 windows 授权之外的所有内容。
问题似乎与 IIS 服务器无法读取网站授权设置或解决方案 webconfig 文件有关。奇怪的是,在服务器本身上,IIS 没有任何错误,但是当我 运行 本地 IIS 服务器上的应用程序时出现错误。
我在 Blazor 服务器端工作,我想访问 windows 用户名。根据文档,我尝试使用 AuthenticationStateProvider
对象执行此操作。问题是当我 运行 解决方案处于发布模式时,用户名 属性 为空。在 Express 模式下它工作正常,我可以获得用户名 属性,在发布模式下该值为空。
这是 AuthenticationStateProvider 对象:
public class Authenticator
{
public readonly AuthenticationStateProvider _auth;
public Authenticator(AuthenticationStateProvider authenticationStateProvider)
{
_auth = authenticationStateProvider;
}
}
我在Startup.cs
中注入它:
services.AddScoped<Authenticator>();
我启用了windows身份验证:
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:49349",
"sslPort": 0
}
}
我是这样使用对象的:
return _auth.GetAuthenticationStateAsync().Result.User.Identity.Name.Substring(3);
我还启用了 WebSocketProtocol:
User对象的Name属性为null,仅在release模式下,在IIS Express的正常Debug模式下,正常运行。
我使用 ASP.NET CORE 3.1 和 VisualStudio 16.9.4。在 ISS 上发布应用程序后,我在 Windows Server 2012 上也遇到了这个问题。
编辑 1:在将 IHttpContextAccessor 与 Blazor-Server 一起使用时,我也遇到了同样的问题。它在 IIS Express 中获取用户名,但在发布模式下,名称为空。
编辑 2:当我 运行 使用应用程序名称而不是 IIS Express 的应用程序时出现问题。从我在网上看到的,这是在背后使用Kestrel服务器。
// Add Identity Order = 1 !important
services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
options.Password.RequiredLength = ConfigIdentity.Password_RequiredLength;
options.Password.RequireLowercase = ConfigIdentity.Password_RequireLowercase;
options.Password.RequireUppercase = ConfigIdentity.Password_RequireUppercase;
options.Password.RequireNonAlphanumeric = ConfigIdentity.Password_RequireNonAlphanumeric;
options.Password.RequireDigit = ConfigIdentity.Password_RequireDigit;
}).AddUserManager<UsersManager>().AddRoleManager<RolesManager>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
// Database Contexts Order = 2 !important
services.AddDbContext<ApplicationDbContext>(ServiceLifetime.Transient);
// 3 : Cookie Options
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = "/Error/AccessDenied";
options.Cookie.Name = "MyAPP";
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.LoginPath = "/";
//options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
options.SlidingExpiration = true;
});
services.AddHttpContextAccessor();
services.AddRazorPages();
services.AddControllers();
services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
如果其他人会遇到这个问题,我设法通过安装 ASP.NET Core 5.0 Runtime (v5.0.6) - Windows Hosting Bundle 和 URL Rewrite Module 使其工作.我还向 NETWORK 和 IIS_IUSRS 用户添加了对发布文件夹的完全权限,并修改了 IIS 网络应用程序授权并禁用了除 windows 授权之外的所有内容。
问题似乎与 IIS 服务器无法读取网站授权设置或解决方案 webconfig 文件有关。奇怪的是,在服务器本身上,IIS 没有任何错误,但是当我 运行 本地 IIS 服务器上的应用程序时出现错误。