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

时间:2017-06-08 14:06:21

标签: c# asp.net asp.net-mvc authentication asp.net-mvc-5

我正在开发一个应用程序,其中未经身份验证的用户将被定向到我的/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配置的应用设置部分

  <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);
        }

    }

1 个答案:

答案 0 :(得分:1)

您需要允许匿名访问您的登录页面。现在,如果尚未登录的用户点击任何页面,包括登录页面,他们将被重定向到登录页面,他们仍然没有登录,因此被重定向再次,一次又一次等等。

您可以使用[Authorize]替换页面上的[AllowAnonymous]属性和控制器中的操作来执行此操作。