forms验证问题

bluebirdlxp 2004-03-22 06:02:58
谁有这方面的资料,或代码
...全文
34 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluebirdlxp 2004-03-22
  • 打赏
  • 举报
回复
说的都不错,给分
520NET 2004-03-22
  • 打赏
  • 举报
回复
login.aspx.cs

//从数据库读取用户的权限
string role=reader.GetInt32(0).ToString();
//产生 Ticket
FormsAuthenticationTicket userTicket=new FormsAuthenticationTicket(1,uid,
DateTime.Now,DateTime.Now.AddMinutes(30),true,role,"login");

//加密票据
string hashUserTicket=FormsAuthentication.Encrypt(userTicket);
//产生新的Cookie
HttpCookie userCookie=new HttpCookie("login",hashUserTicket);
Response.Cookies.Add(userCookie);


//返回用户原来返回的页面
Context.Response.Redirect(Context.Request["ReturnUrl"],true);

global.asax.cs

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpApplication app=(HttpApplication)sender;
HttpContext ctx=app.Context;
//如果验证成功
if(ctx.Request.IsAuthenticated==true)
{
FormsIdentity typeID=(FormsIdentity)ctx.User.Identity;
FormsAuthenticationTicket ticket=typeID.Ticket;
string []role=ticket.UserData.Split(',');
ctx.User=new GenericPrincipal(typeID,role);
}
}

文件夹底下的web.config的培植
<authorization>
<allow roles="1"/>
<deny users="*"/>
</authorization>

我的权限 role 有三种, 为这三个权限建立三个 文件夹

要 导入的命名空间

using System.Web.Security;
using System.Security.Principal;


web.config 的配置
<authentication mode="Forms">
<forms name="login" path="/" loginUrl="login.aspx">
<credentials passwordFormat="Clear">
<user name="guest" password="guest"/>
</credentials>
</forms>


</authentication>

<authorization>
<deny users="?"/>
</authorization>

起始页为 default.aspx, 登陆页面为login.aspx ;

acewang 2004-03-22
  • 打赏
  • 举报
回复
see:
- Role-based Security with Forms Authentication
http://www.codeproject.com/aspnet/formsroleauth.asp
wtadminxjeri 2004-03-22
  • 打赏
  • 举报
回复
页面调用
//取用户名
User.Identity.Name
//注销用户
//将cookies设置为过去的时间,使其失效,从而进行Forms验证
HttpCookie cookie = new HttpCookie(".fuck");
cookie.Expires=Convert.ToDateTime("1980-2-3");
Response.AppendCookie(cookie);
wtadminxjeri 2004-03-22
  • 打赏
  • 举报
回复
using using System.Web.Security;

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,"名称",DateTime.Now,DateTime.Now.AddMinutes(60),false,".fuck");
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,FormsAuthentication.Encrypt(ticket) );
cookie.Expires=DateTime.Now.AddMinutes(60);//过期时间,这里60为分钟数
Response.Cookies.Add(cookie);

wtadminxjeri 2004-03-22
  • 打赏
  • 举报
回复
web.fonfig

<authentication mode="Forms">
<forms name=".fuck" path="/" loginUrl="*.aspx" protection="All" />
</authentication>
<authorization>
<deny users="?"/>
</authorization>

62,046

社区成员

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

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

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

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