C# Asp.Net Core 3.0授权和身份验证
无论我在互联网上看到什么地方,都会在与net.core相关的登录过程中使用标识。没有人谈论使用我们的普通用户名和密码登录。我们正在登录,但这次是通过支票 我们不能使用[Authorize(Roles=“Admin”)]或[Authorize]属性。要使用它,我们需要按如下方式登录 signInManager.PasswordSignInAsync(model.email,model.password,true,true) 看看这个链接,结果是一样的 是 如果结果是这样,我们正在努力。我需要做什么才能在不使用策略的情况下将上述属性用于我自己的登录,signInManager.PasswordSignInAsync正是这样做的,正如我在下面添加到消息中的一样,但它并没有发生 我的登录码 我的创业C# Asp.Net Core 3.0授权和身份验证,c#,authentication,authorization,identity,principal,C#,Authentication,Authorization,Identity,Principal,无论我在互联网上看到什么地方,都会在与net.core相关的登录过程中使用标识。没有人谈论使用我们的普通用户名和密码登录。我们正在登录,但这次是通过支票 我们不能使用[Authorize(Roles=“Admin”)]或[Authorize]属性。要使用它,我们需要按如下方式登录 signInManager.PasswordSignInAsync(model.email,model.password,true,true) 看看这个链接,结果是一样的 是 如果结果是这样,我们正在努力。我需要做什么
services.AddAuthentication(options =>
{
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(config =>
{
config.Cookie.Name = "login";
config.LoginPath = "/Account/Login";
config.ExpireTimeSpan = TimeSpan.FromMinutes(5);
});
我按如下方式解决了这个问题。如果用户名和密码为true
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.UserName));
identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
identity.AddClaim(new Claim(ClaimTypes.GivenName, user.Name));
identity.AddClaim(new Claim(ClaimTypes.Surname, user.Surname));
identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));
foreach (var role in _userManager.GetRolesAsync(user).Result)
{
identity.AddClaim(new Claim(ClaimTypes.Role, role));
}
ClaimsPrincipal principal = new ClaimsPrincipal(identity);
AuthenticationProperties _authentication = new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTimeOffset.UtcNow
};
await _HttpContextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = true });
我的创业
services.AddAuthentication(options =>
{
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(config =>
{
config.Cookie.Name = "login";
config.LoginPath = "/Account/Login";
config.ExpireTimeSpan = TimeSpan.FromMinutes(5);
});
和应用程序
app.UseAuthentication();
app.UseAuthorization();