Below code is really working for login and logout. But i am facing an issue,
- Ran the application with chrome and login with A user. Successfully logged in
- He opened another tab it is not asking for login. Because he already logged in with previous tab, it took that cookies.
- A user navigating to purchase menu in that new tab. He has rights to open this menu and do purchase order.
- He clicked logged out from the first tab and it is successfully logged out. The second tab still opened with purchase screen.
- Now User B Successfully logged in with his credential. He doesn't have purchase activity rights.
- He opened that purchase screen tab and placed an order it is successfully placed
Login code ``` ClaimsIdentity identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, u.Name), new Claim(ClaimTypes.Name, u.DisplayName), new Claim(ClaimTypes.UserData, JsonSerializer.Serialize(u)), }, CookieAuthenticationDefaults.AuthenticationScheme); var principal = new ClaimsPrincipal(identity); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); ``` Logout code ``` public async Task Logout() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return RedirectToAction("Index", "Home"); } ``` Startup.cs ```
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(x =>
{
x.LoginPath = "/UserAccount";
x.ExpireTimeSpan = TimeSpan.FromMinutes(10);
x.SlidingExpiration = true;
});
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
services.AddMvc(options =>
{
options.Filters.Add(new AuthorizeFilter(policy));
});
