我正在处理一个应用程序,在该应用程序中,未身份验证的用户将被直接访问my/account/login.cshtml页面,在其中,他们将被要求使用我的数据库中存储的名称和密码登录。(个人身份验证(。
我做了什么
到目前为止,我已经设置了与数据库的身份验证和连接,并且登录工作正常。但是,当我尝试设置登录页面时,我会重定向到登录。我已将以下内容添加到我的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(不存在的页面(,我会遇到页面错误的错误。所以我想知道我的帐户/登录是否有问题?
我不相信我的登录。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);
}
}
您需要允许匿名访问您的登录页面。现在,如果尚未登录的用户尚未命中任何页面,包括登录页面 ,他们将被重定向到登录页面,在该页面上仍未登录,因此一次又一次地重定向,又一次等等。
您可以通过更换页面上的[Authorize]
属性来做到这一点,并使用[AllowAnonymous]
中的操作。