(转)基于窗体的身份验证
2008-06-28 23:04
225 查看
基于窗体的身份验证
//登陆页面protected void Button1_Click(object sender, EventArgs e)
{
string username = "tsuser";
string userpwd = "resust";
string roles = "Adminstrator";
//生成验证票据对象.
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
username,
DateTime.Now, DateTime.Now.AddMinutes(1),
false,
roles);
//加密验证票
string encrytedTicket = FormsAuthentication.Encrypt(authTicket);
//生成Cookie对象.
//FormsAuthentication.FormsCookieName取得WebConfig中<Authentication>
//配置节中Name的值作为Cookie的名字.
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,
encrytedTicket);
Response.Cookies.Add(authCookie);
//跳转到用户的初试请求页.
string ddd = FormsAuthentication.GetRedirectUrl(username, false);
Response.Redirect( "WebForm1.aspx" );
//Server.Transfer("WebForm1.aspx");
}
//Global.asax中《加上using System.Security.Principal;命名空间》
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//获取用户的角色。
string cookieName = FormsAuthentication.FormsCookieName;//从验证票据获取Cookie的名字。
//取得Cookie.
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if (null == authCookie)
{
return;
}
FormsAuthenticationTicket authTicket = null;
//获取验证票据。
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (null == authTicket)
{
return;
}
//验证票据的UserData中存放的是用户角色信息。
//UserData本来存放用户自定义信息。此处用来存放用户角色。
string[] roles = authTicket.UserData.Split(new char[] { ',' });
FormsIdentity id = new FormsIdentity(authTicket);
GenericPrincipal principal = new GenericPrincipal(id, roles);
//把生成的验证票信息和角色信息赋给当前用户.
Context.User = principal;
}
//不通过验证直接进入主页,会返回到default.aspx页面*/
//也可以在新页面中判断
if ( !HttpContext.Current.User.IsInRole("Adminstrator") )
{
Response.Redirect("Default.aspx");
}
/*//web.config中添加
<authentication mode="Forms">
<forms name="MYWEBAPP.ASPXAUTH"
loginUrl="default.aspx"
protection="All"
path="/"/>
</authentication>
<!-- 设置对本地目录的访问。如果验证票据未通过,则无法访问
-->
<authorization>
<!-- Order and case are important below -->
<allow roles="Adminstrator"/>
<deny users="*"/>
</authorization>
基于窗体的身份验证是一项 ASP.NET 身份验证服务,它使应用程序能够提供自己的登录用户界面并进行自己的凭据验证。ASP.NET 对用户进行身份验证,将未经身份验证的用户重定向到登录页,并执行所有必要的 Cookie 管理。这种身份验证是许多网站使用的流行方法。
应用程序必须配置为使用基于窗体的身份验证,方法是将 <authentication> 设置为 Forms,并拒绝匿名用户访问。下面的示例演示如何在所需应用程序的 Web.config 文件中完成此操作: <configuration><system.web><authentication mode="Forms"/><authorization><deny users="?" /></authorization></system.web></configuration>管理员使用基于窗体的身份验证来配置要使用的 Cookie 的名称、保护类型、用于登录页的 URL、Cookie 有效的时间长度,以及用于已发出的 Cookie 的路径。下表显示 <Forms> 元素的有效属性,该元素是下面的示例中显示的 <authentication> 元素的子元素:
<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>
相关文章推荐
- Forms基于窗体身份验证
- 如何使用 C# .NET 在 ASP.NET 应用程序中实现基于窗体的身份验证
- 如何使用 C# .NET 在 ASP.NET 应用程序中实现基于窗体的身份验证
- ASP.net基于窗体的身份验证
- 如何使用 C# .NET 在 ASP.NET 应用程序中实现基于窗体的身份验证
- ASP.NET 中基于角色的窗体身份验证的研究
- Forms基于窗体身份验证
- ASP.Net:基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- ASP.NET 中基于角色的窗体身份验证的研究
- ASP.Net:基于窗体的身份验证
- 如何使用 C# .NET 在 ASP.NET 应用程序中实现基于窗体的身份验证
- 不规范的命名对ASP.NET中基于窗体的自定义身份验证的影响
- 如何使用 C# .NET 在 ASP.NET 应用程序中实现基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- 通过PowerShell打开Web应用程序的“基于窗体的身份验证”
- 基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- asp.net基于窗体的身份验证