web.config文件
<authentication mode="Forms">
<forms loginUrl="sysadmin/Login.aspx" name=".ASPXFORMSAUTH">
</forms>
</authentication>
新建目录要验证的文件放里面新建一个web.config文件
<?xml version="1.0" encoding="utf-8"?>
<!-- 注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来
配置应用程序的设置。可以使用 Visual Studio 中的“网站”->“ASP.NET 配置”
选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于 /Windows/Microsoft.NET/Framework/v2.0.xxxxx/Config 中
-->
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
//通过验证写Cookie
//方法 a) 指验证后返回请求页面,俗称“从哪来就打哪去”。比如:用户没登录前直接在 IE 地址栏输入 http://localhost/FormTest/UserInfo.aspx ,那么该用户将看到的是 Login.aspx?ReturnUrl=UserInfo.aspx ,输入用户名与密码登录成功后,系统将根据“ReturnUrl”的值,返回相应的页面
//System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.Txt_UserName.Text,false);
//方法 b) 则是分两步走:通过验证后就直接发放 Cookie ,跳转页面将由程序员自行指定,此方法多用于 Default.aspx 使用框架结构的系统。
System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text, false);
Response.Redirect("default.html");
//取当前登录用户
//Response.Write(HttpContext.Current.User.Identity.Name);
//Response.Write(this.Page.User.Identity.Name);