成员资格总是 return false

Membership always return false

我在 c# 应用程序中的成员身份总是 returns false 我在堆栈溢出中阅读了所有类似的问题 我改变了 AppName , passType ..... 但我无法解决它:( , 谢谢你的帮助 , 我的 C# 代码是:

private void BtnEnter_Click(object sender, EventArgs e)
    {            
           bool i = Membership.ValidateUser(txtUserName.Text, txtPass.Text);

            if (i)
            {
                    ClsVariable.UserId=new Guid(Membership.GetUser(txtUserName.Text).ProviderUserKey.ToString());
                    InsertLogin(ClsVariable.UserId.ToString());
                    MainForm fr = new MainForm();
                    fr.Show();
                    Hide();
            }

            else
            {
                MessageBox.Show("username or password is not valid.");
            }
    }

我的 app.config 是:

<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="Accountingf.Properties.Settings.DbAccountingConnectionString"
      connectionString="Data Source=SHIMA-PC;Initial Catalog=DbAccounting;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    <add name="Accountingf.Properties.Settings.DbAccountingConnectionString1"
      connectionString="Data Source=SHIMA-PC;Initial Catalog=DbAccounting;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <membership >
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider"
             type="System.Web.Security.SqlMembershipProvider"
             connectionStringName="Accountingf.Properties.Settings.DbAccountingConnectionString"
             requiresQuestionAndAnswer="false"
             enablePasswordRetrieval="true"
             requiresUniqueEmail="false"
             minRequiredPasswordLength="4"
             minRequiredNonalphanumericCharacters="0"
             passwordStrengthRegularExpression="(?=[a-zA-Z0-9])"
             passwordFormat="Clear"
             applicationName="Accountingf"/>
      </providers>
    </membership>
  </system.web>
</configuration>

我更改了我的 C# 代码并且它有效,所以我的密码和用户名是正确的!

 if(txtUserName.Text=="admin" && txtPass.Text=="1111")
    {
       MainForm fr = new MainForm();
       fr.Show();

使用以下命令直接在数据库上检查用户状态

select * from aspnet_Membership
inner join aspnet_Users on aspnet_users.UserId = aspnet_Membership.UserId 
where aspnet_Users.UserName = 'admin'

例如,您的用户可能被锁定。

您还可以使用所有可用的辅助存储过程来确保您拥有正确的密码。 我的建议是首先确保用户在数据库级别上工作正常,然后尝试在代码级别上找出任何可能出错的地方。