我有一个包含多个身份验证方案的项目。我可以成功登录自定义方案,并在我的标题剃刀视图中调用
User.Identity.IsAuthenticated
并返回true
(最终我希望显示用户名)。
我有一个没有
[Authorize]
属性的不同控制器。我想从第一个/特定/不同的 AuthenticationScheme 中获取登录用户。
如果我在第二页中使用
User.Identity.IsAuthenticated
和未指定方案的不同控制器,我会得到 User.Identity.IsAuthenticated == false
有什么方法可以调用
User.Identity.IsAuthenticated
(并最终获得用户名)以获得非必需的方案吗?
这是在一个 Umbraco v10 网站上,它带有自己的身份验证方案,我不确定这是否是工作中的扳手。
此处的问题已通过正确设置默认身份验证方案得到解决。
这是解决问题的最终配置,特别注意最后几行。我曾尝试过类似的设置,但没有得到正确的确切咒语。
public static IServiceCollection AddCustomMembership(this IServiceCollection services)
{
//authentication definition
//https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-6.0
services
.AddAuthentication(MembershipConstants.AuthenticationScheme)
.AddCookie(
MembershipConstants.AuthenticationScheme,
options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
options.SlidingExpiration = true;
options.LoginPath = new PathString("/api/member/log-in/");
//options.AccessDeniedPath = new PathString("/Forbidden");
}
);
services
.PostConfigure<CookieAuthenticationOptions>(
MembershipConstants.AuthenticationScheme,
option =>
{
option.Cookie.Name = MembershipConstants.AuthenticationCookieName;
}
);
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = MembershipConstants.AuthenticationScheme;
});
services.AddAuthorization();
return services;
}