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