【问题标题】:Asp.net forms authentication for users in sqlserversql server中用户的asp.net表单身份验证
【发布时间】:2012-01-22 06:27:22
【问题描述】:

我想在 ASP.net 中使用表单身份验证。用户在我项目的数据库中。我的代码在下面。但这些代码不起作用。(用户无法登录)。用户位于“新闻”数据库的“用户”表中。 我的 web.config 文件:

    <configuration>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="default.aspx" name=".asp" path="/" timeout="1"   >
      </forms>
    </authentication>
    <authorization>
     <allow users="*"/>
    </authorization>
    <compilation debug="true" targetFramework="4.0"/>
    <membership>
      <providers>
        <clear/>
        <add name="MySqlMembershipProvider"
             connectionStringName="news"
             applicationName="users"
             type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"  />
      </providers>
    </membership>

  </system.web>
  <appSettings/>
  <connectionStrings>
    <add name="news" connectionString="Data Source=Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ava\Desktop\WebSite3\App_Data\news.mdf;Integrated Security=True;User Instance=True"
     providerName="System.Data.SqlClient" />
  </connectionStrings>

  <location path="karbar.aspx">
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
  </location>
</configuration>

我在 default.aspx 表单中的登录代码是:

> protected void Button1_Click(object sender, EventArgs e) {
>     if (FormsAuthentication.Authenticate(user.Text, pass.Text))
>     {
>         FormsAuthentication.SetAuthCookie(user.Text, true);
>         FormsAuthentication.RedirectFromLoginPage(pass.Text, true);
>     }
>     else
>         user.Text = ":((((((("; }

【问题讨论】:

  • 您的Web.Config 中有一个名为news 的连接字符串吗?
  • 对,好像 name="ConnectionString" 应该是 name="news"
  • 是的,我有。它在 标签中
  • 我的用户在新闻数据库的用户表中。这个表需要设置吗?
  • 我把名字改成了 "news" 。但这还不行。当我输入用户并正确传递时,这认为我输入错误

标签: asp.net sql-server visual-studio-2010 forms-authentication


【解决方案1】:

以这种方式更新您的配置文件:

<membership>
  <providers>
    <clear/>
    <add name="MySqlMembershipProvider"
         connectionStringName="ConnectionString"
         applicationName="users"
         type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"  />
  </providers>
</membership>

</system.web>

<appSettings/>
<connectionStrings>
  <add name="ConnectionString" connectionString="Data Source=Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ava\Desktop\WebSite3\App_Data\news.mdf;Integrated Security=True;User Instance=True"
   providerName="System.Data.SqlClient" />
</connectionStrings>

更新 我希望您的代码也有问题,请将其更改为

 if (Membership.ValidateUser(user.Text, pass.Text))
 {
     FormsAuthentication.SetAuthCookie(user.Text, true);
     FormsAuthentication.RedirectFromLoginPage(user.Text, true);
 }

更新:由于您使用的是自己的数据库,因此无需会员资格

 <configuration>
  <system.web>
<authentication mode="Forms">
  <forms loginUrl="default.aspx" name=".asp" path="/" timeout="1"   >
  </forms>
</authentication>
<authorization>
 <allow users="*"/>
</authorization>
<compilation debug="true" targetFramework="4.0"/>

<connectionStrings>
<add name="news" connectionString="Data Source=Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ava\Desktop\WebSite3\App_Data\news.mdf;Integrated Security=True;User Instance=True"
 providerName="System.Data.SqlClient" />
 </connectionStrings>
 <location path="karbar.aspx">
  <system.web>
  <authorization>
    <deny users="?" />
  </authorization>
</system.web>

请同时删除 &lt;membership&gt; 标记。

然后在后面的代码中从数据库中读取用户记录,将结果放入 datareader

protected void Button1_Click(object sender, EventArgs e) {
       //assume you opened the db

 SqlCommand cmd=new SqlCommand();
  cmd.CommandText="select * from users where username=@name and password=@pass";
  cmd.Connection=conn;
  cmd.Parameters.AddWithValue("name",user.Text);
  cmd.Parameters.AddWithValue("pass",pass.Text);
 SqlDataReader dr=cmd.ExecuteReader();
 if (dr.HasRows)
 {
     FormsAuthentication.SetAuthCookie(user.Text, true);
     FormsAuthentication.RedirectFromLoginPage(user.Text, true);
 }
else labelerror.Text="Incorrect name or pass";
}

现在使用您的表名,我希望您知道该怎么做。请记住,您只有一个受保护的 karbar.aspx 网络表单。

【讨论】:

  • 我这样做。但任何用户都无法登录
  • 这需要对数据库进行任何更改吗?我的数据库必须是用户表的特殊格式吗?
  • 你没有配置你的数据库来支持会员制
  • 我的数据库有一个 users 表,它有 2 列:user 和 pass。并且有一个记录
  • @AvaBahari:哦,您已经将数据库配置为支持成员资格。因此,请使用您自己的身份验证。你不需要使用会员资格。也看看我怎么带你去聊天室
【解决方案2】:

两件事:

  1. 正如其他人在 cmets 中提到的那样,确保连接名称是 news不是,而是在 &lt;connectionStrings&gt; 元素中。

  2. 通过运行aspnet_regsql.exe,您的数据库应该有一系列特殊的 talbes+views+SPs of Forms Authentication。这些表由 ASP.NET 的身份验证/成员资格/角色提供程序在内部进行管理。

【讨论】:

  • 我的数据库有一个 users 表,它有 2 列:user 和 pass。并且有一个记录
  • 我还有其他方法吗?
【解决方案3】:

在以下标签中:

<forms loginUrl="default.aspx" name=".[CookieName]" path="/" timeout="1" >

将 cookie 的名称放在 name 属性中。

还有 --> 改变 seconde 参数:

FormsAuthentication.RedirectFromLoginPage(pass.Text, true);

FormsAuthentication.RedirectFromLoginPage(pass.Text, false);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多