forms验证 问题!

pontus 2005-06-15 06:29:08
---web.config
<system.web>
...
<authentication mode="Forms">
<forms name="Catalog.ASPXAUTH " loginUrl="/Manage/login.aspx" protection="All" timeout="30" path= "/">
</forms>
</authentication>
<authorization>
<allow users="*" />
</authorization>
..
</system.web>

<location path="Manage">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>

--------------------------C#
private void btnOK_Click(object sender, System.EventArgs e)
{
string strUserName = Tool.FormatString(this.txtUserName.Text.Trim());
string strPassword = Tool.FormatString(this.txtPassword.Text.Trim());

DataTable dt = CDB.GetUserByLogin(strUserName,strPassword);

if(dt.Rows.Count > 0)
{
FormsAuthentication.SetAuthCookie(strUserName,false);

string returnUrl = Request.QueryString["ReturnUrl"];
if (returnUrl == null) returnUrl = "/";

Response.Redirect(returnUrl);
}
else
{
this.labMessage.Text = "无法登录!";
}

--------------
输入正确后怎么还是不能用过验证阿?老是出现login.aspx页面
...全文
112 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanruinet 2005-06-15
  • 打赏
  • 举报
回复
多了两个空格
<forms name="Catalog.ASPXAUTH " loginUrl="/Manage/login.aspx" protection="All" timeout="30" path= "/">
删掉"Catalog.ASPXAUTH "里面那个空格,和path=后面那个空格
boytomato 2005-06-15
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<system.web>

<!-- 动态调试编译
设置 compilation debug="true" 以启用 ASPX 调试。否则,将此值设置为
false 将提高此应用程序的运行时性能。
设置 compilation debug="true" 以将调试符号(.pdb 信息)
插入到编译页中。因为这将创建执行起来
较慢的大文件,所以应该只在调试时将此值设置为 true,而在所有其他时候都设置为
false。有关更多信息,请参考有关
调试 ASP.NET 文件的文档。
-->
<compilation
defaultLanguage="c#"
debug="true"
/>

<!-- 自定义错误信息
设置 customErrors mode="On" 或 "RemoteOnly" 以启用自定义错误信息,或设置为 "Off" 以禁用自定义错误信息。
为每个要处理的错误添加 <error> 标记。

"On" 始终显示自定义(友好的)信息。
"Off" 始终显示详细的 ASP.NET 错误信息。
"RemoteOnly" 只对不在本地 Web 服务器上运行的
用户显示自定义(友好的)信息。出于安全目的,建议使用此设置,以便
不向远程客户端显示应用程序的详细信息。
-->
<customErrors
mode="RemoteOnly"
/>

<!-- 身份验证
此节设置应用程序的身份验证策略。可能的模式是 "Windows"、
"Forms"、 "Passport" 和 "None"

"None" 不执行身份验证。
"Windows" IIS 根据应用程序的设置执行身份验证
(基本、简要或集成 Windows)。在 IIS 中必须禁用匿名访问。
"Forms" 您为用户提供一个输入凭据的自定义窗体(Web 页),然后
在您的应用程序中验证他们的身份。用户凭据标记存储在 Cookie 中。
"Passport" 身份验证是通过 Microsoft 的集中身份验证服务执行的,
它为成员站点提供单独登录和核心配置文件服务。
-->
<authentication mode="Forms" >

<forms name=".test" loginUrl="Manage/login.aspx" timeout="30" path="/"></forms>

</authentication>

<!-- 授权
此节设置应用程序的授权策略。可以允许或拒绝不同的用户或角色访问
应用程序资源。通配符: "*" 表示任何人,"?" 表示匿名
(未经身份验证的)用户。
-->

<authorization>
<allow users="*" /> <!-- 允许所有用户 -->
<!-- <allow users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
<deny users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
-->
</authorization>

<!-- 应用程序级别跟踪记录
应用程序级别跟踪为应用程序中的每一页启用跟踪日志输出。
设置 trace enabled="true" 可以启用应用程序跟踪记录。如果 pageOutput="true",则
在每一页的底部显示跟踪信息。否则,可以通过浏览 Web 应用程序
根目录中的 "trace.axd" 页来查看
应用程序跟踪日志。
-->
<trace
enabled="false"
requestLimit="10"
pageOutput="false"
traceMode="SortByTime"
localOnly="true"
/>

<!-- 会话状态设置
默认情况下,ASP.NET 使用 Cookie 来标识哪些请求属于特定的会话。
如果 Cookie 不可用,则可以通过将会话标识符添加到 URL 来跟踪会话。
若要禁用 Cookie,请设置 sessionState cookieless="true"。
-->
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
cookieless="false"
timeout="20"
/>

<!-- 全球化
此节设置应用程序的全球化设置。
-->
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>

</system.web>

<location path="Manage">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>

</configuration>


Manage 目录下有 login.aspx 和 default .aspx 你设置 default.aspx 为首页....
login.aspx

private void Button1_Click(object sender, System.EventArgs e)
{
System.Web.Security .FormsAuthentication.RedirectFromLoginPage ("abcv",false);

}

我试了一下是通能验证的....
pontus 2005-06-15
  • 打赏
  • 举报
回复
我要的就是有个Manage的文件夹需要验证了才能看到里面的文件

可是现在怎么都通不过验证,FormsAuthentication.SetAuthCookie都已经执行到了

不知到我的web.config设置对没有?
pontus 2005-06-15
  • 打赏
  • 举报
回复
上面的方法试过了,一样的结果!
hackate 2005-06-15
  • 打赏
  • 举报
回复
一个简单的例子给你看看.一般如果你用forms验证的话捏.比如你访问项目下的你的随便的.aspx文件,如果没有通过,就会跳转到login.aspx页面.然后在这个页面里你可以这样处理.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text.Trim <> "" And TextBox2.Text.Trim <> "" Then
If TextBox1.Text = "hackate" And TextBox2.Text = "520520" Then
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, CheckBox1.Checked) ''这个方法设置了,如果验证成功,就自动返回在转到登陆页面前的页面,这样他会自动转过去,
Else
Response.Write("<script>alert('用户名或者密码错误!');</script>")
End If
Else
Response.Write("<script>alert('不能为空');</script>")
End If
End Sub
wangjuping 2005-06-15
  • 打赏
  • 举报
回复
<forms name="Catalog.ASPXAUTH " loginUrl="/Manage/login.aspx" protection="All" timeout="30" path= "/">
</forms>

我用这句时说是无法识别的属性!
boytomato 2005-06-15
  • 打赏
  • 举报
回复
通过验证后 .....


// Create a new ticket used for authentication
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version
Username.Text, // Username associated with ticket
DateTime.Now, // Date/time issued
DateTime.Now.AddMinutes(30), // Date/time to expire
true, // "true" for a persistent user cookie
reader.GetString(0), // User-data, in this case the roles
FormsAuthentication.FormsCookiePath);// Path cookie valid for

// Encrypt the cookie using the machine key for secure transport
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName, // Name of auth cookie
hash); // Hashed ticket

// Set the cookie's expiration time to the tickets expiration time
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

// Add the cookie to the list for outgoing response
Response.Cookies.Add(cookie);

// Redirect to requested URL, or homepage if no previous page
// requested
string returnUrl = Request.QueryString["ReturnUrl"];
if (returnUrl == null) returnUrl = "/";

// Don't call FormsAuthentication.RedirectFromLoginPage since it
// could
// replace the authentication ticket (cookie) we just added
Response.Redirect(returnUrl);

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧