[英]After SignIn User.Identity.Name is empty, but User.Identity.IsAuthenticated is set right
应用程序在 Razor 页 ASP.NET 内核中开发
我以这种方式通过 LDAP 登录用户:
public async Task<IActionResult> OnPostAsync()
{
var user = authService.Login(UserName, Password);
if (null != user)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, UserName),
};
ClaimsIdentity identity = new ClaimsIdentity(claims, "Cookies", "user", "role");
await HttpContext.SignInAsync(new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookies", "user", "role")));
}
else
{
return Unauthorized();
}
return Redirect("~/Index");
}
这就是 Startup.cs 的外观
services.Configure<LdapConfig>(Configuration.GetSection("Ldap"));
services.AddScoped<IAuthenticationService, LdapAuthenticationService>();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.LoginPath = "/Account/SignIn";
});
services
.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = "TicketTracker.ldap.identity";
options.Cookie.HttpOnly = true;
options.Cookie.Expiration = TimeSpan.FromDays(150);
options.LoginPath = "/Account/Signin"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
options.LogoutPath = "/Account/Signout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
options.SlidingExpiration = true;
options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
});
关于身份验证一切正常,但我无法访问已登录的当前用户的名称。
有人知道该怎么做吗?
谢谢!
问题解决了自己感谢没有帮助!
options.Cookie.Name = "TicketTracker.ldap.identity";
就在这一行,我只是将其替换为
options.Cookie.Name = ".AspNetCore.Cookies";
因为 cookie 的名称是由我通过 SignInAsync 方法传递的声明身份创建的。
现在工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.