Ald*_*jal 5 c# asp.net asp.net-mvc claims-based-identity asp.net-core-2.0
最近,我使用个人身份验证启动了Asp.Net Core 2.0 MVC项目。ConfigureServices如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DBConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,退出后User.Identity.IsAuthenticated和_signInManager.IsSignedIn(User)仍然为true,无论我是否这样做:await _signInManager.SignOutAsync();或by await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);并且无论我是否关闭并稍后在浏览器中重新打开应用程序。
我总是需要这样做,如果有人返回索引应用程序或关闭并在浏览器中重新打开该应用程序,则系统会退出以前打开的任何会话并强制登录。
我尝试执行以下操作,但未成功!
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> Login()
{
var auth = User.Identity.IsAuthenticated; // Still true
if (_signInManager.IsSignedIn(User)) // Still true
{
await _signInManager.SignOutAsync();
//HttpContext = new DefaultHttpContext
//{
// User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, "username") }, ""))
//};
}
else
{
// Clear the existing external cookie to ensure a clean login process
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1100 次 |
| 最近记录: |