.net Core如何使用User.Identity.IsAuthenticated
.net Core how to use User.Identity.IsAuthenticated
我一步步使用 MSDB 创建身份验证 cookie
第一步.设置start.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
CookieAuthenticationDefaults.AuthenticationScheme;
);
services.AddDbContext<CoreContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DB")));
services.AddMvc();
}
步骤 2. 向 ClaimsIdentity() 添加声明
var claims = new List<Claim>
{
new Claim(ClaimTypes.MobilePhone, ""),
new Claim(ClaimTypes.Name, "")
};
var userIdentity = new ClaimsIdentity("Custom");
userIdentity.AddClaims(claims);
ClaimsPrincipal userPrincipal = new ClaimsPrincipal(userIdentity);
HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal);
获得 User.Identity.IsAuthenticated
状态的其他操作的最后一步
但状态总是错误如何解决问题?
你可以试试这个
var claims = new List<Claim>
{
new Claim("user", ""),
new Claim("role", "Member")
};
HttpContext.SignInAsync(new ClaimsPrincipal(new ClaimsIdentity(claims, SysBase.cookieName, "user", "role")));
我一步步使用 MSDB 创建身份验证 cookie
第一步.设置start.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
CookieAuthenticationDefaults.AuthenticationScheme;
);
services.AddDbContext<CoreContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DB")));
services.AddMvc();
}
步骤 2. 向 ClaimsIdentity() 添加声明
var claims = new List<Claim>
{
new Claim(ClaimTypes.MobilePhone, ""),
new Claim(ClaimTypes.Name, "")
};
var userIdentity = new ClaimsIdentity("Custom");
userIdentity.AddClaims(claims);
ClaimsPrincipal userPrincipal = new ClaimsPrincipal(userIdentity);
HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal);
获得 User.Identity.IsAuthenticated
状态的其他操作的最后一步
但状态总是错误如何解决问题?
你可以试试这个
var claims = new List<Claim>
{
new Claim("user", ""),
new Claim("role", "Member")
};
HttpContext.SignInAsync(new ClaimsPrincipal(new ClaimsIdentity(claims, SysBase.cookieName, "user", "role")));