我们正在使用System.Web.Security.FormsAuthenticationTicket为未登录的用户创建匿名cookie。AspNetCore是否具有等效功能?我知道ASP.NET Core不支持表单身份验证。做事的新方法是cookie。那么,如何创建在新情况下等效的cookie?
Asp.net核心不支持表单身份验证。我建议你使用基于cookie的身份验证。该链接可以帮助你构建它。
如果要跳过需要授权访问的方法。你可以添加attribute [AllowAnonymous]
。
[AllowAnonymous]
public IActionResult Privacy()
{
return View();
}
或者,你可以参考此链接。
在Startup.cs中配置cookie。
services.AddAuthentication("auth")
.AddCookie("auth",config=>
{
config.Cookie.Name = "cookie.name";
config.LoginPath = "/home/login";
});
在此操作中生成令牌。你可以通过接收表单数据来填写索赔。
[HttpPost]
public IActionResult login()
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name,"myName"),
new Claim(ClaimTypes.Role,"myRole")
};
var claimIdentity = new ClaimsIdentity(claims,"id card");
var claimPrinciple = new ClaimsPrincipal(claimIdentity);
var authenticationProperty = new AuthenticationProperties
{
IsPersistent = true
};
HttpContext.SignInAsync(claimPrinciple,authenticationProperty);
return View();
}
@MrFox,您可以接受答案吗?
当然,我正在寻找Cookie的替代方案。我将编辑原始问题。
@MrFox,添加cookie配置,然后生成令牌。
@MrFox,您可以接受此答案,还是有其他问题?