为什么ASP.NET FormsAuthentication饼干不是认证用户? [英] Why is ASP.NET FormsAuthentication cookie not authenticating user?

查看:262
本文介绍了为什么ASP.NET FormsAuthentication饼干不是认证用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用默认的SqlMembershipProvider和FormsAuthentication的网站。我可以使用内置的登录控制和/或在程序调用的所有方法来验证用户,并得到同样的结果 - 用户进行身份验证和cookie被创建,但因为我可以cookie不似乎是有效的吨得到进入需要身份验证的任何页面。

I have a site that uses the default SqlMembershipProvider and FormsAuthentication. I can use the built-in Login Controls and/or programmatically call all the methods to authenticate a user and get the same result - the user is authenticated and a cookie is created, but the cookie does not appear to be valid since I can't get into any page that requires authentication.

有没有真正的code,以显示默认的登录控制,因为它应该只是工作,但这里是自定义code我尝试:

There is no real code to show for the default Login Control since it should just "work", but here is the custom code I tried:

protected void ctrlLogin_Authenticate(object sender, AuthenticateEventArgs e)
{
   if (Membership.ValidateUser(ctrlLogin.UserName, ctrlLogin.Password))
   {
      FormsAuthentication.RedirectFromLoginPage(ctrlLogin.UserName, ctrlLogin.RememberMeSet);
      /*
       * I also tried this:
      FormsAuthentication.SetAuthCookie(ctrlLogin.UserName, ctrlLogin.RememberMeSet);
      if (!String.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
         Response.Redirect(Request.QueryString["ReturnUrl"]);
      Response.Redirect("/index.aspx");
       */
   }
   else
   {
      ctrlLogin.FailureText = "Invalid Username/Password Combination";
   }
}

有了这个code, Membership.ValidateUser()成功,无一不 FormsAuthentication.RedirectFromLoginPage()和FormsAuthentication.RedirectFromLoginPage()成功设置cookie - 这饼干只是不工作来验证我的身份验证。我已删除我的所有Cookie,看着他们得到与FireCookie再次创造证实了这一点。 cookie的名称匹配什么,我有我的web.config,域名为/,并如预期的到期日(见下文)。

With this code, Membership.ValidateUser() succeeds, and both FormsAuthentication.RedirectFromLoginPage() and FormsAuthentication.RedirectFromLoginPage() successfully set a cookie - that cookie just doesn't work to verify my authentication. I have confirmed this by deleting all my cookies and watching them get created again with FireCookie. The cookie name matches what I have in my web.config, the domain is "/", and the expiration date is as expected (see below).

下面是我的web.config的有关章节:

Here are the relevant sections of my web.config:

<authentication mode="Forms">
  <forms loginUrl="~/login/index.aspx" name=".SoeAuth" protection="All"
    slidingExpiration="true" timeout="525599" domain=""></forms>
</authentication>
<membership defaultProvider="SqlMembershipProvider">
  <providers>
    <add connectionStringName="[MY_CS]" applicationName="[MY_APPNAME]"
      minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0"
      enablePasswordReset="true" passwordFormat="Hashed" requiresUniqueEmail="true"
      name="SqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider"
      requiresQuestionAndAnswer="false"/>
  </providers>
</membership>

应当注意的是,我还添加在我的web.config文件中的machineKey条目基于从<一个建议href=\"http://stackoverflow.com/questions/3953212/why-isnt-aspaux-cookie-being-validated-by-formsauthentication\">a非常类似的问题在这里(没有解决我的问题)。此外,以供参考,超时= 525599以上不到一年的时间为1分钟我的持久cookie。

It should be noted that I also added a machineKey entry in my web.config file based on a suggestion from a very similar question here (which didn't solve my problem). Also, for reference, the timeout=525599 above is 1 minute less than a year for my persistent cookies.

推荐答案

我发现这个问题:

由于我是能够创建具有完全相同的源$ C ​​$ C简单的工作测试项目,我确定,问题是在web.config文件中。

Since I was able to create a simple working test project with the exact same source code, I determined that the problem was in the web.config file.

要通过每个部分,我在'的System.Web /的HttpModules部分我有一个&LT发现元素。这样取出&LT;添加名称=FormsAuthenticationTYPE =System.Web.Security.FormsAuthenticationModule/&GT; 在计算机级别的web.config文件中定义的模块。添加回在瞬间解决了这一问题。

Going through each section, I discovered in the 'system.web / httpModules' section I had a <clear/> element. This removed the <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"/> module defined in machine-level web.config file. Adding it back in instantly fixed the problem.

这肯定将是很好得到一个错误信息,当我试图使用FormsAuthentication方法和模块甚至没有加载中...

这篇关于为什么ASP.NET FormsAuthentication饼干不是认证用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆