簡體   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