I am using MVC5 identity for user login. Currently I using to logout an user if he/she inactive for specific period of time. Now I want to redirect him/her on the same page from which it logout; after the login once again. I am not sure whether it possible or not. Here is my Login ActionResult
public ActionResult Login(string username, string password)
{
var user = new UserManager().IsValid(username, password);
if (user!=null)
{
var ident = new ClaimsIdentity(
new[] {
// adding following 2 claim just for supporting default antiforgery provider
new Claim(ClaimTypes.NameIdentifier, username),
new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),
new Claim(ClaimTypes.Name,username),
new Claim(ClaimTypes.Sid,user.UserId.ToString()),
// optionally add roles if any
new Claim(ClaimTypes.Role,user.OperationType),
//new Claim(ClaimTypes.Role, "User"),
},
DefaultAuthenticationTypes.ApplicationCookie);
var claimsPrincipal = new ClaimsPrincipal(ident);
// Set current principal
Thread.CurrentPrincipal = claimsPrincipal;
HttpContext.GetOwinContext().Authentication.SignIn(
new AuthenticationProperties { IsPersistent = false }, ident);
UserManager userManager=new UserManager();
userManager.GetUserMenu();
return RedirectToAction("Index","Home"); // auth succeed
}
// invalid username or password
ModelState.AddModelError("", "invalid username or password");
return View();
}
And my Startup Class class where I set mechanism for logout after a time period and all other,
public class Startup
{
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
CookieSecure = CookieSecureOption.SameAsRequest,
LoginPath = new PathString("/Account/Login") ,
LogoutPath = new PathString("/Account/Logout"),
SlidingExpiration = true,
Provider = new CookieAuthenticationProvider
{
OnResponseSignIn = context =>
{
context.Properties.AllowRefresh = true;
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(1); //for test purpose
}
},
ReturnUrlParameter = ""
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ApplicationCookie);
}
}
My question is: Is it possible if someone get logout for say home/test
with /Account/Login?=%2Fhome%2Ftest
as returnurl and then login once again, could it redirect him/her to home/test
rather than home/index
? If yes, then how could I achieve this?
Once you are logged out and redirected past that point you no longer have any reference to the previous url. When you hit your logout method though you have access to that. Store that in a cookie, check if it exists in your login method, and if it does expire the cookie and send them to the stored route.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.