[英]How to redirect user to same page from which it's logout due to login expire in MVC C#?
I am using MVC5 identity for user login. 我正在使用MVC5身份进行用户登录。 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, 还有我的Startup Class类,我设置了一段时间后的注销机制,以及其他所有设置,
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
? 我的问题是:是否可能有人用/Account/Login?=%2Fhome%2Ftest
登出说“ home/test
/Account/Login?=%2Fhome%2Ftest
作为returnurl然后再次登录,是否可以将他/她重定向到“ home/test
而不是“ 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. 一旦您注销并重定向到该点,您将不再对以前的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. 将其存储在cookie中,检查它是否存在于您的登录方法中,并且是否确实使cookie过期并将其发送到存储的路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.