简体   繁体   English

成功认证后,Asp MVC URL 不会重定向到索引

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

In my ASP MVC web application, when I try to authenticate using email and password to log in. The URL redirect doesn't allow me to pass to the main page 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);
    }  

and my routeConfig code:和我的 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 }
        );
    }
}

When I enter on login page the URL on localhost is something like this: http://localhost:3535/account/login?ReturnUrl=%2F当我进入登录页面时,本地主机上的 URL 是这样的: http://localhost:3535/account/login?ReturnUrl=%2F

When I fill the login form with the correct credentials I got this: http://localhost:3535/account/login?ReturnUrl=%2Fhome%2FManager当我用正确的凭据填写登录表单时,我得到了这个:http://localhost:3535/account/login?ReturnUrl=%2Fhome%2FManager

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

About OwinAuthentication , using external login to authenticate such as Google and Microsoft, both works without any issue, I only got a problem on manual login.关于OwinAuthentication ,使用外部登录进行身份验证,例如谷歌和微软,都没有任何问题,我只是在手动登录时遇到了问题。

I have found that the solution to solve this issue is creating a Custom Authentication and Authorization for my custom local login.我发现解决此问题的解决方案是为我的自定义本地登录创建自定义身份验证和授权。

My custom login only updates session and redirects to account/index, which probably requires authentication, thus the redirect to the authentication url.我的自定义登录仅更新 session 并重定向到帐户/索引,这可能需要身份验证,因此重定向到身份验证 url。

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

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