编程语言
首页 > 编程语言> > c# – ASP.NET Membership.ValidateUser()总是返回“false”

c# – ASP.NET Membership.ValidateUser()总是返回“false”

作者:互联网

参见英文答案 > Membership.ValidateUser always return false on IIS                                    2个
这是我的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("user@mail.com", "pass123");

当我在数据库上执行选择时,我得到了正确的用户.

用户isAproved = true

用户isLockedout = 0

解决方法:

配置ASP.NET 2.0 Membership和其他Providers时,需要设置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>

你可以尝试在这里获得价值

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

或者打开ASPNETDB数据库中的aspnet_Users和aspnet_Applications表,找出在开发过程中创建用户和其他数据时使用的应用程序名称(查看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-provider
来源: https://codeday.me/bug/20190528/1174768.html