简体   繁体   English

如何在MVC C#中将用户重定向到由于登录到期而退出登录的页面?

[英]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.

相关问题 如何根据用户在Asp.net C#中的角色将用户从登录页面重定向到下一页 - how to redirect user from login page to next page base on their Role in Asp.net C# 注销后,如何在ASP.NET MVC3中单击浏览器的后退按钮时将用户重定向到登录页面 - After logout,how to redirect user to login page when he/she will click back button of browser in ASP.NET MVC3 如何将用户从登录表单重定向到c#中的另一个表单 - How to Redirect The User From Login Form to Another Form in c# 单击注销时如何重定向到登录页面 - How to Redirect to Login page while click on logout 如何在mvc c#中重定向用户? - How to redirect user in mvc c#? 超时和强制注销时将用户重定向到登录页面 - Redirect user to login page on timeout and when forced logout MVC在同一用户登录后注销所有活动会话 - MVC logout all active sessions after same user login 从 Twitter API 获取用户的电子邮件以进行外部登录身份验证 ASP.NET MVC C# - Get user's email from Twitter API for External Login Authentication ASP.NET MVC C# 如何随着会话过期或放弃将用户重定向到特定页面 - How to redirect user to specific page as Session Expire or abandon MVC C#重定向到登录模式 - MVC C# Redirect to Login Modal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM