Forms Authentication 概述

来源:互联网 发布:mac wine 玩LOL 编辑:程序博客网 时间:2024/05/01 13:51

一、Forms Authentication 机制

 在解释Forms Authentication之前,我们不得不简要说明一下web身份验证的三种方式:Windows 验证,Forms验证,Passport验证。在这里解释一下Windows身份验证,windows验证的工作机制如下:

当一个匿名请求到达服务器时,服务器会发送一个http response,浏览器会以呈现一个模态窗口的方式要求用户输入用户凭证,然后把输入的身份信息放在http header 中发送给服务器,服务器会查询数据库进行验证,一般来说服务器需要有一个windows账户数据库,所以这种验证方式一般只在内部网络使用。

ok,现在我们就讲一下forms authentication的工作机制:

当一个request过来的时候,首先由forms authenticaton system对这个请求进行身份验证,如果这个请求是未经验证的,那么该请求是匿名请求;之后请求会到达authorize system,它会查询配置文件检查此次请求的资源是否对该请求可见,如果是可见的,则该请求是经过授权的,即可以访问该资源,如果是不可见的,那么由authorize system往回发送一个401状态码,forms authentication system会自动识别这个状态码,然后产生一个302状态码,这个状态码表示资源重定向,即此时forms authentication会设置redirect to 登陆页面(这个登陆页面的url会在配置文件中设置),此时转到登陆页面,用户输入身份信息,再次经过上述过程,不过此时信息经过验证之后,服务器会为客户端产生一个forms authentication ticket,这个ticket会包含用户的身份信息,服务器把这个ticket返回到客户端,并且把用户redirect到一开始要访问的页面,那么浏览器把刚刚拿到的ticket放在http header中,再次请求资源,这时候forms authentication system就能找到ticket,用户就会得到验证,如果用户得到授权的话就可以访问到资源了,以上就是详细的工作流程了......表达能力有限,说的可能有点不太明白。

下面是图解........

二、forms authentication 配置

在讲配置之前,首先要创建一个web.config文件,如果有的话就不需要了.........

配置forms authentication相应元素是<authentication>,验证的方式是由它的属性mode设置,在这里设置成Forms就ko了,注意大小写

具体的配置都是在<forms>这个子元素的属性中设置的,以下是对这些属性的说明:

cookieless

This attribute specifies under what conditions the authentication ticket is stored in a cookie versus being embedded in the URL. Allowable values are: UseCookies; UseUri; AutoDetect; and UseDeviceProfile (the default). Step 2 examines this setting in more detail.

defaultUrl

Indicates the URL that users are redirected to after signing in from the login page if there is no RedirectUrl value specified in the querystring. The default value is default.aspx.

domain

When using cookie-based authentication tickets, this setting specifies the cookie's domain value. The default value is an empty string, which causes the browser to use the domain from which it was issued (such as www.yourdomain.com). In this case, the cookie will not be sent when making requests to subdomains, such as admin.yourdomain.com. If you want the cookie to be passed to all subdomains you need to customize the domain attribute setting it to yourdomain.com .

enableCrossAppRedirects

A Boolean value indicating whether authenticated users are remembered when redirected to URLs in other web applications on the same server. The default is false.

loginUrl

The URL of the login page. The default value is login.aspx.

name

When using cookie-based authentication tickets, the name of the cookie. The default is .ASPXAUTH .

path

When using cookie-based authentication tickets, this setting specifies the cookie's path attribute. The path attribute enables a developer to limit the scope of a cookie to a particular directory hierarchy. The default value is / , which informs the browser to send the authentication ticket cookie to any request made to the domain.

protection

Indicates what techniques are used to protect the forms authentication ticket. The allowable values are: All (the default); Encryption; None; and Validation. These settings are discussed in detail in Step 3.

requireSSL

A Boolean value that indicates whether an SSL connection is required to transmit the authentication cookie. The default value is false.

slidingExpiration

A Boolean value that indicates whether the authentication cookie's timeout is reset each time the user visits the site during a single session. The default value is true. The authentication ticket timeout policy is discussed in more detail in the Specifying the Ticket's Timeout Value section.

timeout

Specifies the time, in minutes, after which the authentication ticket cookie expires. The default value is 30. The authentication ticket timeout policy is discussed in more detail in the Specifying the Ticket's Timeout Value section.

 

更详细的配置说明上网查一下吧..............

三、一些常用的方法以及属性

在创建forms authentication ticket时,可以用FormsAuthentication的三个方法:

GetAuthCookie(UserName,isPersist);

SetAuthCookie(UserName,isPersist);

RedirectFromLoginPage(UserName,isPersist)。

这里假设配置中的cookieless的设置是基于cookie的,第一个方法返回一个ticket,第一个方法把产生的ticket加入到cookiecollection中,第三个方法调用第二个方法,并且重定向到returnUrl指定的那个页面。

在验证完成之后,可能想访问用户的信息,可以通过HttpContext的User属性,User属性其实是一个实现接口IPrincipal的对象,它有Identity属性和IsInRole(string name)方法,Identity属性是一个实现了接口IIdentity的对象,这个对象包含了用户的身份信息,也可以包含一些自定义数据。

如要访问用户名称:

 

User.Identity.Name;

 要查看请求是否经过验证了,可以用如下语句

User.Idnentity.IsAuthenticated ;

也可以用

Request.IsAuthenticated;

原创粉丝点击