繁体   English   中英

应用程序重定向到login.aspx页面

[英]Application redirects to login.aspx page

我正在开发一个应用程序,其中未经身份验证的用户将被定向到我的/Account/login.cshtml页面,在该页面上将要求他们使用存储在我的数据库中的名称和密码登录。 (个人身份验证)。

我做了什么

到目前为止,我已经建立了身份验证并连接到数据库,并且登录正常。 但是,当我尝试设置登录页面时,我被重定向到login.aspx。 我已经将以下内容添加到我的webconfig中,并且发生了一些奇怪的事情。

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login"  />
</authentication>

有了上面的内容,当我运行程序时,这就是我被重定向到的内容。

http://localhost:64998/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/Account/Login?ReturnUrl=/

但是,如果我将链接更改为/ Account / test(不存在的页面),则会收到该页面的错误,不符合我的预期。 所以我想知道我的帐户/登录是否有问题?

我不认为我的login.cshtml页面有什么问题,因为如果我明确地调用它,则可以正常登录。 当我未通过身份验证时尝试重定向到此页面时,会发生问题。

关于在哪里观看的任何帮助将不胜感激。

更新在帐户控制器中登录

   public UserManager<ApplicationUser> UserManager { get; private set; }

        //
        // GET: /Account/Login
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {

Web Config的“应用程序设置”部分

  <appSettings>
    <add key="PreserveLoginUrl" value="true" />
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

 <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

进一步测试

经过更多测试后,我意识到注释掉以下代码会使应用程序正常工作。 但是我仍然不认为这是一种解决方案,因为我的会话不会过期。

代码是问题在我的FilterConfig中,并在RegisterGlobalFilters中调用

  public class SessionExpireFilterAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext ctx = HttpContext.Current;
            // check if session is supported
            if (ctx.Session != null)
            {

                // check if a new session id was generated
                if (ctx.Session.IsNewSession)
                {

                    // If it says it is a new session, but an existing cookie exists, then it must
                    // have timed out
                    string sessionCookie = ctx.Request.Headers["Cookie"];
                    if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
                    {
                        string redirectOnSuccess = filterContext.HttpContext.Request.Url.PathAndQuery;
                        string redirectUrl = string.Format("?ReturnUrl={0}", redirectOnSuccess);
                        string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;
                        if (ctx.Request.IsAuthenticated)
                        {
                            FormsAuthentication.SignOut();
                            HttpContext.Current.Session.Abandon();

                            // clear authentication cookie
                            HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
                            cookie1.Expires = DateTime.Now.AddYears(-1);
                            HttpContext.Current.Response.Cookies.Add(cookie1);

                            // clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
                            HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
                            cookie2.Expires = DateTime.Now.AddYears(-1);
                            HttpContext.Current.Response.Cookies.Add(cookie2);
                        }
                        RedirectResult rr = new RedirectResult(loginUrl);
                        filterContext.Result = rr;
                        //ctx.Response.Redirect("~/Home/Logon");
                    }
                }
            }

            base.OnActionExecuting(filterContext);
        }

    }

您需要允许匿名访问您的登录页面。 现在,如果尚未登录的用户点击了任何页面( 包括登录页面) ,他们将被重定向到登录页面,在该页面上他们仍未登录,因此被一次又一次地重定向,依此类推。

您可以通过使用[AllowAnonymous]替换页面上的[Authorize]属性和控制器中的操作来实现。

暂无
暂无

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

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