标签:持久 add configure 应该 允许 iss session def 调用顺序
Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties)
//获取到用户信息
var user = await _userService.UserLogin(vm);
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, user.UserId),
new Claim(ClaimTypes.Name, user.UserName),
new Claim("UserDataInfo", user.ToJson()),
new Claim(ClaimTypes.Role, "Administrator"),
};
var claimsIdentity = new ClaimsIdentity(claims, Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
//应该允许刷新身份验证会话。
AllowRefresh = false,
//认证票据过期的时间。
// 一个value将覆盖ExpireTimeSpan选项
//CookieAuthenticationOptions设置AddCookie。
ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
//身份验证会话是否持久化
// 多个请求。当与cookie、控件一起使用时
//是否cookie的生存期是绝对的(匹配
//认证票据的生命周期)或基于会话的。
IsPersistent = false,
//颁发身份验证票据的时间。
IssuedUtc = DateTimeOffset.UtcNow,
//作为http的完整路径或绝对URI
//重定向响应值。
RedirectUri = "/Admin/User/Login"
};
await HttpContext.SignInAsync(Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);
在Startup.cs
文件中
public void ConfigureServices(IServiceCollection services){
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>
{
o.LoginPath = new PathString("/manage/home/Login");
o.AccessDeniedPath = new PathString("/Error/Forbidden");
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//注意app.UseAuthentication方法一定要放在下面的app.UseMvc方法前面,否者后面就算调用HttpContext.SignInAsync进行用户登录后,使用
//HttpContext.User还是会显示用户没有登录,并且HttpContext.User.Claims读取不到登录用户的任何信息。
//这说明Asp.Net OWIN框架中MiddleWare的调用顺序会对系统功能产生很大的影响,各个MiddleWare的调用顺序一定不能反
app.UseAuthentication();
app.UseAuthorization();
}
完成
标签:持久 add configure 应该 允许 iss session def 调用顺序
原文地址:https://www.cnblogs.com/LuoEast/p/14784729.html