This question already has answers here:
Membership.ValidateUser always return false on IIS

(2个答案)


5年前关闭。




这是我的web.config:
<membership defaultProvider="CustomizedMembershipProvider">
  <providers>
    <clear />
    <add name="CustomizedMembershipProvider"
         connectionStringName="MYbdName"
         applicationName="/" type="System.Web.Security.SqlMembershipProvider"
         requiresQuestionAndAnswer="false"
         passwordFormat="Clear"
         enablePasswordRetrieval="true"
         requiresUniqueEmail="true"
         minRequiredPasswordLength="4"
         minRequiredNonalphanumericCharacters="0" />
  </providers>
</membership>

我什至硬编码了用户名和密码:
 bool b = Membership.ValidateUser("[email protected]", "pass123");

当我对数据库执行选择时,我得到了正确的用户。

用户isAproved = true

用户isLockedout = 0

最佳答案

配置ASP.NET 2.0成员资格和其他提供程序时,需要设置applicationName属性。在您的web.config中,它丢失了:

<membership defaultProvider="CustomizedMembershipProvider">
  <providers>
    <clear />
    <add name="CustomizedMembershipProvider"
         connectionStringName="MYbdName"
         applicationName="/"   <----------   Missing applicationName
         type="System.Web.Security.SqlMembershipProvider"
         requiresQuestionAndAnswer="false"
         passwordFormat="Clear"
         enablePasswordRetrieval="true"
         requiresUniqueEmail="true"
         minRequiredPasswordLength="4"
         minRequiredNonalphanumericCharacters="0" />
  </providers>
</membership>

您可以尝试在此处获取值(value)
public bool Login(string userName, string password)
{
    var provider = Membership.Provider;
    string name = provider.ApplicationName; // Get the application name here

    return Membership.ValidateUser(userName, password);
}

或在aspnet_Users数据库中打开aspnet_ApplicationsASPNETDB表,并弄清在开发过程中创建用户和其他数据时使用了什么应用程序名称(查看aspnet_Application表以解决此问题)。

然后在您的web.cofig中正确设置属性:
<membership defaultProvider="CustomizedMembershipProvider">
      <providers>
        <clear />
        <add name="CustomizedMembershipProvider"
             connectionStringName="MYbdName"
             applicationName="MyAppName"   <----------   correct
             type="System.Web.Security.SqlMembershipProvider"
             requiresQuestionAndAnswer="false"
             passwordFormat="Clear"
             enablePasswordRetrieval="true"
             requiresUniqueEmail="true"
             minRequiredPasswordLength="4"
             minRequiredNonalphanumericCharacters="0" />
      </providers>
    </membership>

有关更多详细信息,请从Scott-Gu的博客中阅读此article

关于c# - ASP.NET Membership.ValidateUser()始终返回 "false",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22098194/

10-12 20:49