【发布时间】: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