簡體   English   中英

成功認證后,Asp MVC URL 不會重定向到索引

[英]Asp MVC URL doesn't redirect to index after successful authentication

在我的 ASP MVC web 應用程序中,當我嘗試使用 email 和密碼進行身份驗證時登錄。URL 重定向成功后不允許我通過主頁。

    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        if (!string.IsNullOrWhiteSpace(returnUrl) && Url.IsLocalUrl(returnUrl) && returnUrl.Contains(nameof(windowsLogOff)))
        {
            return RedirectToAction(nameof(Login));
        }

        if (User.Identity.IsAuthenticated)
        {
            return RedirectToAction(nameof(windowsLogOff), new { returnUrl = returnUrl });
        }

        if (OwinAuthentication.AuthenticationTypes._ActiveAuthenticationsList.Count == 1 && Portal.Commons.Models.Configuration.ByPassAuthentication)
        {
            return RedirectToAction(nameof(ExternalLoginRedirect), new { returnUrl = returnUrl, provider = OwinAuthentication.AuthenticationTypes._ActiveAuthenticationsList[0].AuthenticationTypeDefault });
        }

        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    [AllowAnonymous]
    public ActionResult Login(LoginViewModel model)
    {
        if (ModelState.IsValid)
        {
            using (var db = new appDbContext())
            {
                var encodedPWD = Sha256(model.Password);
                var obj = db.Users.Where(a => a.Email.Equals(model.Email) && a.PasswordHash.Equals(encodedPWD)).FirstOrDefault();
                if (obj != null)
                {
                    Session["id"] = obj.Id.ToString();
                    Session["name"] = obj.name.ToString();
                    Session["email"] = obj.Email.ToString();

                    return RedirectToAction("Manager", "home");
                }

                ModelState.AddModelError("", "Email or Password is invalid!.");
            }
        }
        return View(model);
    }  

和我的 routeConfig 代碼:

   public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "DefaultEn",
            url: "en/{controller}/{action}/{id}",
            defaults: new { language = "en", controller = "data", action = "index", id = UrlParameter.Optional },
            constraints: new { controller = "data" },
            namespaces: new[] { "Portal.Controllers" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

當我進入登錄頁面時,本地主機上的 URL 是這樣的: http://localhost:3535/account/login?ReturnUrl=%2F

當我用正確的憑據填寫登錄表單時,我得到了這個:http://localhost:3535/account/login?ReturnUrl=%2Fhome%2FManager

而不是:http://localhost:3535/account/Manager

關於OwinAuthentication ,使用外部登錄進行身份驗證,例如谷歌和微軟,都沒有任何問題,我只是在手動登錄時遇到了問題。

我發現解決此問題的解決方案是為我的自定義本地登錄創建自定義身份驗證和授權。

我的自定義登錄僅更新 session 並重定向到帳戶/索引,這可能需要身份驗證,因此重定向到身份驗證 url。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM