利用web.config文件對整個程序集進(jìn)行配置
單個頁面授權(quán)設(shè)置代碼如下:<!-- 只對Default.aspx這個頁面授權(quán)訪問--><location path="Default.aspx"> <!--location指定了配置權(quán)限對應(yīng)的頁面--> <system.web> <authorization> <allow users="*"/> <!--允許所有的人訪問--> </authorization></system.web><location>
在Web程序里,每個根目錄都有一個web.config。但一般的程序只有一個web.config配置文件。有多個文件夾里,子目錄的web.config繼承了母目錄中的web.config。如果子母有沖突的話,則以子目錄的web.config的規(guī)則為準(zhǔn)。
一個網(wǎng)站不允許兩種訪問方式。
Context.User.Identity.Name;//用于獲取當(dāng)前用戶名
Form的驗證方式不理會Windows用戶,全取決于你是否登錄,寫入Cookie。
using System.Web.Security;
FormAuthentication.RedirectFormLoginPage(string userName,bool createPersistentCookie)
//此方法將經(jīng)過身份驗證的用戶重定向回最初請求的URL或默認(rèn)的URL。
createPersistentCookie為true時則會創(chuàng)建一個Cookie。在Windows系統(tǒng)里C:\Documents and Settings\Administrator\Cookies路徑可以找到。
Forms驗證配置說明
loginUrl="login.aspx" [string] 指向登錄頁面。
defaultUrl="default.aspx" [string] 指定用戶通過驗證之后默認(rèn)重定向的URL地址。
protection="All" [All | None |Encryption |Validation] 針對Cookie信息進(jìn)行加密設(shè)置。
timeout="30" [in Minutes][number] 指定Cookie的有效期,以分鐘為單位。
path="/" [string] 為由應(yīng)用程序發(fā)出的Cookie指定路徑。
requireSSL="false" [true | false] 指定是否使用安全套階層(SSL)連接來傳輸身份驗證Cookie。
slidingExpiration="true" [true |false] 指定是否啟用滑動到期策略。
enableCrossAppRedirects="false" [true | false] 指定是否需要將通過身份驗證的用戶重定向到其他Web應(yīng)用程序中URL。
Cookieless="UseDeviceProfile" [UseUri:指示不使用Cookie; |UseCookies:指示一定使用Cookie; |AutoDetect:指示根據(jù)瀏覽器具體情況使用Cookie或不使用; |UseDeviceProfile:與AutoDetect功能類似,但不會對瀏覽器進(jìn)行檢測其是否啟用了Cookie。] 指定是否使用Cookie以及使用的方式。
domain=" " [string]> <credentials passwordFormat="SHA1" [Clear | SHA1 |MD5]> <user name="" [string,Required,Collection Key] password="" [string,Required] /> </credentials></forms></authentication> FormsAuthenticationTicket FormsAuthentication.Encrypt()寫入Cookie時,一定要寫入時限。如: Cookie cook1=new Cookie(); cook1.Expires=DateTime.Now.AddDays(1);
網(wǎng)站有不同角色的用戶,前臺的用戶不能訪問后臺,管理員哪個頁面都可以訪問。
在子文件Web.config里面如果有和同時存在時,必須在前面,否則deny會沖掉allow。