应用程序在Razor Pages ASP中开发。网络核心
我通过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方法传递的声明标识创建的。
现在可以正常工作了