<authentication mode="Forms">
<forms name=".ASPXCOOKIEDEMO" loginUrl="login.aspx" defaultUrl="default.aspx"
protection="All" timeout="30" path="/" requireSSL="false"
slidingExpiration="true" enableCrossAppRedirects="false"
cookieless="UseDeviceProfile" domain="">
<!-- protection="[All|None|Encryption|Validation]" -->
<!-- cookieless="[UseUri | UseCookies | AutoDetect | UseDeviceProfile]" -->
</forms>
</authentication>
cookieless | ASP.NET 2.0 Forms 身份验证可以将 Forms 身份验证票存储在 Cookie 中也可以以无 Cookie 的表示形式存储在 URL 上。UseDeviceProfile 的默认值表示 ASP.NET 根据预先计算得到的浏览器配置文件来确定存储票证的位置。AutoDetect 选项使 ASP.NET 动态确定浏览器是否支持 Cookie。UseUri 和 UseCookies 分别强制实施无 Cookie 票证和有 Cookie 票证。 |
defaultUrl | 指定在成功登录后,请求将重定向到的默认 URL。在登录后进行重定向时,如果重定向 URL 对于 Forms 身份验证不可用,则使用此值。 |
domain | 指定包含 Forms 身份验证票的 HttpCookie 的 Domain 属性的值。显式设置此属性可使应用程序共享同一个 Cookie,前提是这些应用程序共享某个 DNS 命名空间的一个公共部分(例如,如果 domain 属性设置为“contoso.com”,则 appA.contoso.com 和 appB.contoso.com 可以共享一个 Cookie)。 |
enableCrossAppRedirects | 在 ASP.NET 2.0 中,Forms 身份验证允许以查询字符串变量或窗体 POST 变量的形式在应用程序之间传递 Forms 身份验证票。将此属性设置为 true 可使 FormsAuthenticationModule 能够从查询字符串或窗体 POST 变量提取票证。 |
loginUrl | 指定未经身份验证的用户的请求将被重定向到的 URL。该 URL 可以在同一台计算机上或在远程计算机上。如果是在远程计算机上,则两台计算机上 machineKey 配置元素中的 decryptionkey 和 validationKey 属性都需要使用相同的值。 |
name | 用于身份验证的 HTTP Cookie 的名称。注意,如果多个应用程序需要在一台计算机上使用基于窗体的身份验证服务,并且每个应用程序都希望由应用程序隔离 Forms 身份验证 Cookie,则每个应用程序都应配置一个唯一的 Cookie 值。为避免在 URL 中产生依赖项,在设置身份验证 Cookie 时,ASP.NET 还使用“/”作为 Path 值,以便将这些 Cookie 发送回站点上的每个应用程序。 |
path | 用于发出的 Cookie 的路径。默认值为“/”,以避免路径中大小写不匹配的造成的困难,因为在返回 Cookie 时,浏览器是严格区分大小写的。共享服务器环境中的应用程序应使用此指令来维护专用 Cookie。(它们还可以使用 API 在运行时指定路径来发出 Cookie。) |
protection | 用于保护 Cookie 数据的方法。有效值如下: All:同时使用数据验证和加密来保护 Cookie。所配置的数据验证算法是基于 <machinekey> 元素的。如果密钥足够长(48 个字符),默认情况下将使用 AES 进行加密。All 是默认(和建议)值。 None:用于仅将 Cookie 用于个性化设置并且安全性要求不高的站点。加密和验证都可以被禁用。尽管以此方式使用 Cookie 需谨慎,但对于使用 .NET Framework 实现个性化设置的任何方法,此设置提供了最佳性能。 Encryption:使用 AES、TripleDES 或 DES 加密 Cookie,但不对 Cookie 进行数据验证。这类 Cookie 容易受到精心选择的纯文本的攻击。 Validation:不加密 Cookie 的内容,但验证 Cookie 数据在传输过程中是否未被更改。若要创建 Cookie,验证密钥在缓冲区中与 Cookie 数据连接,并且计算出 MAC 并将其追加到输出的 Cookie。 |
requireSSL | 如果设置为 true,则 Forms 身份验证会设置 Forms 身份验证 Cookie 的安全位。兼容的浏览器只将 Cookie 通过 SSL 连接发送回 ASP.NET。注意,如果使用无 Cookie Forms 身份验证,则此设置无效。 |
slidingExpiration | 如果设置为 true,则 Forms 身份验证将定期更新 Forms 身份验证票的生存期。无论票证是包含在 Cookie 中,还是以无 Cookie 的格式包含在 URL 中,都会进行此操作。 |
timeout | 时间量(以整数分钟为单位),经过该时间量之后,Cookie 则会过期。默认值是 30。超时属性是一个可调值,从收到上次请求的时间开始计算,它将在 n 分钟后过期。为了避免对性能产生负面影响,也为了避免那些打开了 Cookie 警告的应用程序产生多个浏览器警告,Cookie 在超时时间过半时更新。(这意味着在某些情况下可能会出现精度损失。) |
例:定义两个input#text 将其源码中加入runat="server" 将text2 type更改为password,再添加一个Button和一个checkBox, 双击button添加Click事件,并加入以下代码:
if( (Text1.Value =="aaa")&&(Text2.Value=="bbb"))
{
FormsAuthentication.RedirectFromLoginPage(Text1.Value,CheckBox1.Checked);//FormsAuthentication,为 Web 应用程序管理 Forms 身份验证服务
}
在web.config中加入
<system.web>
<authentication mode="Forms">
<forms name=".ASPXUSERDEMO" loginUrl="Default2.aspx" protection="All" timeout="60" />
</authentication>
<authorization>
<deny users="?" />//不允许匿名登陆
</authorization>
在系统中有一个User,它有方法User.Identity.Name提许出当前登陆的用户名
以下内容未经例子证明,还可以使用配置为基于窗体的身份验证服务提供有效凭据的列表,如下例所示:
<authentication>
<credentials passwordFormat="SHA1" >
<user name="Mary" password="94F85995C7492EEC546C321821AA4BECA9A3E2B1"/>
<user name="John" password="5753A498F025464D72E088A9D5D6E872592D5F91"/>
</credentials>
</authentication>
还可以通过使用 FormsAuthentication.HashPasswordForStoringInConfigFile(String password, String passwordFormat) API 生成密码的哈希表示形式。此方法支持使用 SHA1 或 MD5 生成哈希值。然后,应用程序可以调用 FormsAuthentication.Authenticate 并提供用户名和密码,ASP.NET 将验证凭据。根据 passwordFormat 属性的下列值,凭据可以存储在明文中,也可以存储为 SHA1 或 MD5:
Hash 类型 说明
Clear 密码以明文形式存储
SHA1 密码存储为 SHA1 摘要
MD5 密码存储为 MD5 摘要
授权用户和角色
ASP.NET用于控制对 URL 资源的客户端访问。它对于用于生成请求的 HTTP 方法(GET 或 POST)是可配置的,并且可配置为允许或拒绝访问用户组或角色组。下面的示例演示向名为 someone 的用户和名为 Admins 的角色授予访问权。所有其他用户的访问被拒绝。
<authorization> <allow users="someone@www.contoso.com" /> <allow roles="Admins" /> <deny users="*" />//*代表所有用户 ?代码匿名用户 </authorization>
成员资格”功能围绕两个中心类来生成:Membership 和 MembershipUser
Membership:例子
MembershipCreateStatus result;//返回值,用做处理创建成功与否返回消息
Membership.CreateUser(username.Text, userpassword.Text, email.Text, DropDownList1.SelectedValue.ToString(), quest.Text, true, out result);
if (result ==MembershipCreateStatus.Success) lblResults.Text = "User successfully created!";
//MembershipUser(有一个功能锁定) MembershipUser Userer;
Userer = Membership.GetUser();
Label1.Text = Userer.UserName;
Label3.Text = Userer.Email;
Label4.Text = Userer.PasswordQuestion;
Label5.Text = Userer.LastPasswordChangedDate.ToString();
Label6.Text = Userer.LastLoginDate.ToString();
<location path="secured">//路径为secured <system.web> <authorization> <deny users="?"/>//所有匿名用户不能访问 <allow users="*"/>//所有登陆用户可以访问 </authorization> </system.web> </location>
二.管理角色Roles
Roles.CreateRole
DE>Roles.DeleteRoleDE>
DE>Roles.GetAllRolesDE>
<location path="administrators_role"> <system.web> <authorization> <allow roles="Administrators" />//Administrators级别用户访问 <deny users="*"/>//所有用户不能访问 </authorization> </system.web> </location>