Reputation: 369
I am new in ASP.NET CORE, so please understand me.
I am currently developing a Login and Logout Process for my application in .Net Core.
My problems are:
These are my ideas but I don't know how to do it.
Confirm Form Resubmission
and Redirect him to Login where he can login their Account.Here is my code:
LoginController.cs
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Login(LoginModel userLogin)
{
ILogicInterface<UserInput, SystemResult> dbLogic = new LoginLogic();
UserInput userInput = new UserInput();
userInput[typeof(LoginModel).FullName] = userLogin;
SystemResult systemResult = dbLogic.DoProcess(userInput);
bool userExist = systemResult.ResultCode
== SystemResult.RESULT_CODE_SUCCESS;
if (userExist)
{
LoginInfomation loginInfomation =
systemResult[typeof(LoginInfomation).FullName] as LoginInfomation;
HttpContext.Session.SetString("userInfo"
, JsonConvert.SerializeObject(loginInfomation));
Claim[] claims = new[] {
new Claim(ClaimTypes.Name, loginInfomation.E_mail)
, new Claim(ClaimTypes.Role
, AccountInformation.GetRole(loginInfomation.AccountInfo.roleID))
};
ClaimsIdentity identity =
new(claims, CookieAuthenticationDefaults.AuthenticationScheme);
AuthenticationHttpContextExtensions.SignInAsync(HttpContext
, new ClaimsPrincipal(identity));
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError(string.Empty
, "The specified email or password is incorrect.");
return View(userLogin);
}
}
HomeController.cs
public IActionResult Logout()
{
AuthenticationHttpContextExtensions.SignOutAsync(HttpContext
, CookieAuthenticationDefaults.AuthenticationScheme);
return RedirectToAction("Login", "Login");
}
Startup.cs
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(10);
options.Cookie.IsEssential = true;
});
services.ConfigureApplicationCookie(options => {
options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
options.LoginPath = new PathString("/Login/Login");
});
Can someone help me to achieve the ideas listed above. Thank you and Regards,
Upvotes: 3
Views: 11024
Reputation: 574
Try adding the customHeaders in in web.config:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0, s-maxage=0" />
<add name="Pragma" value="no-cache" />
<add name="Expires" value="0" />
</customHeaders>
</httpProtocol>
</system.webServer>
Upvotes: 3
Reputation: 1447
Please Try Once this
In Startup.cs
services.AddSession(options =>
{
options.Cookie.Name = "ExampleSession";
options.IdleTimeout = TimeSpan.FromMinutes(10);
options.Cookie.IsEssential = true;
});
In Controller
public async Task<IActionResult> Logout()
{
try
{
await HttpContext.SignOutAsync("ExampleSession");
return RedirectToAction("Login", "Login");
}
catch (Exception ex)
{
throw ex;
}
}
Upvotes: 1