快捷搜索: 王者荣耀 脱发

ASP.NET Web.config 安全配置(用户登录)

别人空间讲解:

在webconfig中有一个重要节点 <system.web> 其中包括了一个身份验证配置节点 <authentication mode="Windows"/> 其中 authentication 是节点名,mode是模式,上述代码表示 采用windows身份验证,那么此时身份验证将交给iis处理,而iis中默认设 <authentication mode="Forms" > <forms defaultUrl="default.aspx" loginUrl="login.aspx" protection="All" timeout="60" name=".king" ></forms> </authentication> <authorization> <deny users="?"/> </authorization> a、使mode为 Forms 则表示是用Forms身份验证 b、defaultUrl ="default.aspx" 表示默认页面是default.aspx c、loginUrl="login.aspx" 表示登陆页面是login.aspx d、protection="All" 表示 保护所有页面,但不能保护html页面,只能保护aspx页面。 e、<authorization> 节点中 deny users="?" 表示拒绝所有匿名用户,也就是说必须通过验证的用户才可以跳转到默认页或由程序指定一个跳转页,其中 "?" 表示匿名用户,也可以设置为 "*" 表示所有用户,或则指定一个用户名。 f、timeout 表示有效时间。 在登陆页面中,登陆按钮事件必须使用如下代码才能跳转 FormsAuthentication.RedirectfromLoginPage("luby", false); 引用的命名空间是 System.Web.Security; RedirectfromLoginPage表示将经过身份验证的用户重定向到最初请求的页面,第一参数表示名称,此名称今后在本站点的任何页面都可以用 User.Identity.Name 来获取到,第二个参数表示是否要持久化cookie,也就是说是否下次不需要输入密码验证就可以自动登陆,直到用户主动退出。 退出按钮必须使用如下代码,命名空间同样是 System.Web.Security; FormsAuthentication.SignOut();

自个实验:

web.config页:

<authentication mode="Forms" > <forms name="wjy" loginUrl="login.aspx"> <credentials passwordFormat="Clear"> <user name="admin" password="admin"/> <user name="wjy" password="wjy"/> </credentials> </forms> </authentication> <authorization> <deny users="?"/> </authorization>

login.aspx页面:

using System.Web.Security; //这个命名空间是下面类FormsAuthentication

public partial class login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {

} protected void Button1_Click(object sender, EventArgs e) { if (FormsAuthentication.Authenticate(TextBox1.Text, TextBox2.Text)) { FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);//上面有讲解 Response.Redirect("Default2.aspx"); } else Response.Write("Sorry"); } }

Default2.aspx页:

using System.Web.Security;

public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string name = string.Format("你输入的名字为{0}", User.Identity.Name);//可以传过来用户名 Response.Write(name); } protected void logout_Click(object sender, EventArgs e) //注销代码 { FormsAuthentication.SignOut(); Response.Redirect("Default.aspx"); } }

经验分享 程序员 微信小程序 职场和发展