<authentication mode="Forms"> <forms name=".ASPXAUTH" loginUrl="Login.aspx" protection="All" path="/" timeout="20"/> </authentication> <authorization> <deny users="?"></deny> </authorization>
<configuration> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </configuration>
<location path="Register.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
private void Btn_Login_Click(object sender, System.EventArgs e)
{
if(this.Txt_UserName.Text=="Admin" && this.Txt_Password.Text=="123456")
{
FormsAuthentication.RedirectFromLoginPage(this.Txt_UserName.Text,false);
}
}
private void Btn_Login_Click(object sender, System.EventArgs e)
{
if(this.Txt_UserName.Text=="Admin" && this.Txt_Password.Text=="123456")
{
FormsAuthentication.SetAuthCookie(this.Txt_UserName.Text,false);
Response.Redirect("Default.aspx");
}
}
[Serializable]
public class UserInfo
{
//用户登录信息
private int _nId;
private string _sRealName;
private string _sName;
private string _sPassword;
private string _sRoles;
public int Id
{
get { return this._nId; }
set { this._nId = value; }
}
public string RealName
{
get { return this._sRealName; }
set { this._sRealName = value; }
}
public string Name
{
get { return this._sName; }
set { this._sName = value; }
}
public string Password
{
get { return this._sPassword; }
set { this._sPassword = value; }
}
public string Roles
{
get { return this._sRoles; }
set { this._sRoles = value; }
}
public UserInfo()
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text == "Admin" && this.TextBox2.Text == "123456")
{
// 加密UserInfo
UserInfo user = new UserInfo();
user.Id = 1;
user.Name = this.TextBox1.Text;
user.Password = this.TextBox2.Text;
user.RealName = "系统管理员";
user.Roles = "Administrators,Users";
string strUser = Serialize.Encrypt<UserInfo>(user);
// 设置Ticket信息
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, user.Name, DateTime.Now, DateTime.Now.AddMinutes(20), false, strUser);
// 加密验证票据
string strTicket = FormsAuthentication.Encrypt(ticket);
// 使用新userdata保存cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, strTicket);
cookie.Expires = ticket.Expiration;
this.Response.Cookies.Add(cookie);
this.Response.Redirect("Default.aspx");
}
}
public FormsAuthenticationTicket( int version, //版本号 string name, //与身份验证票关联的用户名 DateTime issueDate, //票据的发出时间 DateTime expiration,//票据的到期日期 bool isPersistent, //票据是否存储在持久的 Cookie 中,是为 true;否则为 false string userData //票据中存储的用户定义数据 );
public class LoginBasePage : Page
{
protected UserInfo LoginUser
{
get
{
string strUser = ((FormsIdentity)this.Context.User.Identity).Ticket.UserData;
return Serialize.Decrypt<UserInfo>(strUser);
}
}
public LoginBasePage()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<authorization>
<allow users="Admin"></allow>
<deny users="*"></deny>
</authorization>
</system.web>
</configuration>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<authorization>
<allow roles="Users"></allow>
<deny users="*"></deny>
</authorization>
</system.web>
</configuration>
if (this.TextBox1.Text == "User1" && this.TextBox2.Text == "111111")
{
// 加密UserInfo
UserInfo user = new UserInfo();
user.Id = 2;
user.Name = this.TextBox1.Text;
user.Password = this.TextBox2.Text;
user.RealName = "普通用户1";
user.Roles = "Users";
string strUser = Serialize.Encrypt<UserInfo>(user);
// 设置Ticket信息
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, user.Name, DateTime.Now, DateTime.Now.AddMinutes(20), false, strUser);
// 加密验证票据
string strTicket = FormsAuthentication.Encrypt(ticket);
// 使用新userdata保存cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, strTicket);
cookie.Expires = ticket.Expiration;
this.Response.Cookies.Add(cookie);
this.Response.Redirect("Default.aspx");
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (this.Context.User != null)
{
if (this.Context.User.Identity.IsAuthenticated)
{
if (this.Context.User.Identity is FormsIdentity)
{
string strUser = ((FormsIdentity)this.Context.User.Identity).Ticket.UserData;
string[] roles = Serialize.Decrypt<UserInfo>(strUser).Roles.Split(',');
this.Context.User = new GenericPrincipal(this.Context.User.Identity, roles);
}
}
}
}
<configuration>
<appSettings/>
<connectionStrings/>
<location path ="Register.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path ="ManageAdmin">
<system.web>
<authorization>
<allow roles="Administrators"></allow>
<deny users="*"></deny>
</authorization>
</system.web>
</location>
<location path ="ManageUsers">
<system.web>
<authorization>
<allow roles="Users"></allow>
<deny users="*"></deny>
</authorization>
</system.web>
</location>
<system.web>
<!-- 这里放置原来根目录 Web.config 的内容,就不列出来了 -->
</system.web>
</configuration>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有