【发布时间】:2021-07-21 06:11:51
【问题描述】:
在我的 ASP MVC Web 应用程序中,当我尝试使用电子邮件和密码进行身份验证登录时。成功身份验证后,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,使用外部登录验证,如谷歌和微软,都没有任何问题,我只是手动登录有问题。
【问题讨论】:
-
改变这个 return RedirectToAction("Manager", "home");返回 RedirectToAction("Manager", "account"); ?
-
@Adlorem 我在家庭控制器上有 Manager ActionResult,但也尝试使用帐户控制器上的一个 ActionResult 进行测试,但没有工作,遇到了同样的问题。也许问题可能出在 http post 上的 Session 上,但仍然无法弄清楚
标签: c# asp.net asp.net-mvc owin