繁体   English   中英

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

[英]How to redirect user to same page from which it's logout due to login expire in MVC C#?

我正在使用MVC5身份进行用户登录。 目前,如果用户在特定时间段内处于非活动状态,我通常会注销该用户。 现在,我要在退出页面的同一页面上重定向他/她; 登录后再次。 我不确定是否可以。 这是我的登录操作结果

    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();
    }

还有我的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);
}
}

我的问题是:是否可能有人用/Account/Login?=%2Fhome%2Ftest登出说“ home/test /Account/Login?=%2Fhome%2Ftest作为returnurl然后再次登录,是否可以将他/她重定向到“ home/test而不是“ home/index 如果是,那我该如何实现呢?

一旦您注销并重定向到该点,您将不再对以前的URL有任何引用。 当您单击注销方法时,尽管您可以访问它。 将其存储在cookie中,检查它是否存在于您的登录方法中,并且是否确实使cookie过期并将其发送到存储的路由。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM