
{"id":469,"date":"2006-08-29T01:13:00","date_gmt":"2006-08-29T01:13:00","guid":{"rendered":"http:\/\/www.sincebirth.cn\/?p=469"},"modified":"2006-08-29T01:13:00","modified_gmt":"2006-08-29T01:13:00","slug":"aspnet%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%9f%ba%e4%ba%8e%e8%a7%92%e8%89%b2role%e7%9a%84forms%e9%aa%8c%e8%af%81","status":"publish","type":"post","link":"https:\/\/niuduer.sincebirth.space\/?p=469","title":{"rendered":"asp.net\u4e2d\u4f7f\u7528\u57fa\u4e8e\u89d2\u8272role\u7684Forms\u9a8c\u8bc1"},"content":{"rendered":"<div id=\"blog_text\" class=\"cnt\">\n<h2 >asp.net\u4e2d\u4f7f\u7528\u57fa\u4e8e\u89d2\u8272role\u7684Forms\u9a8c\u8bc1\uff0c\u5927\u81f4\u7ecf\u8fc7\u51e0\u4e0b\u56db\u6b65\uff1a<br \/><strong >1.\u914d\u7f6e\u7cfb\u7edfweb.config<\/strong ><\/h2 >\n<div class=\"postText\" >\n<div style=\"BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; BORDER-BOTTOM-WIDTH: 1px; BORDER-BOTTOM-COLOR: #cccccc; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: bre\" ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >system<\/span ><span style=\"COLOR: #ff0000\" >.web<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><span style=\"COLOR: #000000\" >&nbsp;<br \/><\/span ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >authentication&nbsp;<\/span ><span style=\"COLOR: #ff0000\" >mode<\/span ><span style=\"COLOR: #0000ff\" >=&#8221;Forms&#8221;<\/span ><span style=\"COLOR: #ff0000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><span style=\"COLOR: #000000\" >&nbsp;<br \/>&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >forms&nbsp;<\/span ><span style=\"COLOR: #ff0000\" >name<\/span ><span style=\"COLOR: #0000ff\" >=&#8221;.yaoCookies&#8221;<\/span ><span style=\"COLOR: #ff0000\" >&nbsp;loginUrl<\/span ><span style=\"COLOR: #0000ff\" >=&#8221;\/duan\/Manage\/login.aspx&#8221;<\/span ><span style=\"COLOR: #ff0000\" >&nbsp;protection<\/span ><span style=\"COLOR: #0000ff\" >=&#8221;All&#8221;<\/span ><span style=\"COLOR: #ff0000\" ><br \/>&nbsp;&nbsp;timeout<\/span ><span style=\"COLOR: #0000ff\" >=&#8221;20&#8243;<\/span ><span style=\"COLOR: #ff0000\" >&nbsp;path<\/span ><span style=\"COLOR: #0000ff\" >=&#8221;\/&#8221;<\/span ><span style=\"COLOR: #ff0000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >\/&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >&lt;\/<\/span ><span style=\"COLOR: #800000\" >authentication<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/><\/span ><span style=\"COLOR: #0000ff\" >&lt;\/<\/span ><span style=\"COLOR: #800000\" >system.web<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><\/div >\n<p >\u5176\u4e2d&lt;authentication mode= &#8220;forms&#8221;&gt; \u8868\u793a\u672c\u5e94\u7528\u7a0b\u5e8f\u91c7\u7528Forms\u9a8c\u8bc1\u65b9\u5f0f\u3002<br \/>1). &lt;forms&gt;\u6807\u7b7e\u4e2d\u7684name\u8868\u793a\u6307\u5b9a\u8981\u7528\u4e8e\u8eab\u4efd\u9a8c\u8bc1\u7684 HTTP Cookie\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cname \u7684\u503c\u662f .ASPXAUTH\u3002\u91c7\u7528\u6b64\u79cd\u65b9\u5f0f\u9a8c\u8bc1\u7528\u6237\u540e,\u4ee5\u6b64\u7528\u6237\u7684\u4fe1\u606f\u5efa\u7acb\u4e00\u4e2aFormsAuthenticationTicket\u7c7b\u578b\u7684\u8eab\u4efd\u9a8c\u8bc1\u7968,\u518d\u52a0\u5bc6\u5e8f\u5217\u5316\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32,\u6700\u540e\u5c06\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5199\u5230\u5ba2\u6237\u7aef\u7684name\u6307\u5b9a\u540d\u5b57\u7684Cookie\u4e2d.\u4e00\u65e6\u8fd9\u4e2aCookie\u5199\u5230\u5ba2\u6237\u7aef\u540e,\u6b64\u7528\u6237\u518d\u6b21\u8bbf\u95ee\u8fd9\u4e2aweb\u5e94\u7528\u65f6\u4f1a\u5c06\u8fde\u540cCookie\u4e00\u8d77\u53d1\u9001\u5230\u670d\u52a1\u7aef,\u670d\u52a1\u7aef\u5c06\u4f1a\u77e5\u9053\u6b64\u7528\u6237\u662f\u5df2\u7ecf\u9a8c\u8bc1\u8fc7\u7684. <\/p >\n<p >2). &lt;forms&gt;\u6807\u7b7e\u4e2d\u7684loginUrl\u6307\u5b9a\u5982\u679c\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u6709\u6548\u7684\u8eab\u4efd\u9a8c\u8bc1 Cookie\uff0c\u4e3a\u767b\u5f55\u5c06\u8bf7\u6c42\u91cd\u5b9a\u5411\u5230\u7684 URL\u3002\u9ed8\u8ba4\u503c\u4e3a default.aspx\u3002loginUrl\u6307\u5b9a\u7684\u9875\u9762\u5c31\u662f\u7528\u6765\u9a8c\u8bc1\u7528\u6237\u8eab\u4efd\u7684,\u4e00\u822c\u6b64\u9875\u9762\u63d0\u4f9b\u7528\u6237\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801,\u7528\u6237\u63d0\u4ea4\u540e\u7531\u7a0b\u5e8f\u6765\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u6765\u9a8c\u8bc1\u7528\u6237\u7684\u5408\u6cd5\u6027(\u5927\u591a\u60c5\u51b5\u662f\u5c06\u7528\u6237\u8f93\u5165\u4fe1\u606f\u540c\u6570\u636e\u5e93\u4e2d\u7684\u7528\u6237\u8868\u8fdb\u884c\u6bd4\u8f83),\u5982\u679c\u9a8c\u8bc1\u7528\u6237\u6709\u6548,\u5219\u751f\u6210\u540c\u6b64\u7528\u6237\u5bf9\u5e94\u7684\u8eab\u4efd\u9a8c\u8bc1\u7968,\u5199\u5230\u5ba2\u6237\u7aef\u7684Cookie,\u6700\u540e\u5c06\u6d4f\u89c8\u5668\u91cd\u5b9a\u5411\u5230\u7528\u6237\u521d\u8bd5\u8bf7\u6c42\u7684\u9875\u9762.\u4e00\u822c\u662f\u7528FormsAuthentication.RedirectFromLoginPage \u65b9\u6cd5\u6765\u5b8c\u6210\u751f\u6210\u8eab\u4efd\u9a8c\u8bc1\u7968,\u5199\u56de\u5ba2\u6237\u7aef,\u6d4f\u89c8\u5668\u91cd\u5b9a\u5411\u7b49\u4e00\u7cfb\u5217\u7684\u52a8\u4f5c.<\/p >\n<p >public static void RedirectFromLoginPage( string userName, bool createPersistentCookie, string strCookiePath );<\/p >\n<p >\u5176\u4e2d:<br \/>userName\uff1a \u5c31\u662f\u6b64\u7528\u6237\u7684\u6807\u793a,\u7528\u6765\u6807\u5fd7\u6b64\u7528\u6237\u7684\u552f\u4e00\u6807\u793a,\u4e0d\u4e00\u5b9a\u8981\u6620\u5c04\u5230\u7528\u6237\u8d26\u6237\u540d\u79f0.<br \/>createPersistentCookie\uff1a \u6807\u793a\u662f\u5426\u53d1\u51fa\u6301\u4e45\u7684 Cookie\u3002<br \/>\u82e5\u4e0d\u662f\u6301\u4e45Cookie\uff0cCookie\u7684\u6709\u6548\u671fExpiration\u5c5e\u6027\u6709\u5f53\u524d\u65f6\u95f4\u52a0\u4e0aweb.config\u4e2dtimeout\u7684\u65f6\u95f4\uff0c\u6bcf\u6b21\u8bf7\u6c42\u9875\u9762\u65f6\uff0c\u5728\u9a8c\u8bc1\u8eab\u4efd\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u5224\u65ad\u662f\u5426\u8fc7\u4e86\u6709\u6548\u671f\u7684\u4e00\u534a\uff0c\u8981\u662f\u7684\u8bdd\u66f4\u65b0\u4e00\u6b21cookie\u7684\u6709\u6548\u671f\uff1b\u82e5\u662f\u6301\u4e45cookie\uff0cExpiration\u5c5e\u6027\u65e0\u610f\u4e49\uff0c\u8fd9\u65f6\u8eab\u4efd\u9a8c\u8bc1\u7968\u7684\u6709\u6548\u671f\u6709cookie\u7684Expires\u51b3\u5b9a\uff0cRedirectFromLoginPage\u65b9\u6cd5\u7ed9Expires\u5c5e\u6027\u8bbe\u5b9a\u7684\u662f50\u5e74\u6709\u6548\u671f\u3002<br \/>strCookiePath\uff1a \u6807\u793a\u5c06\u751f\u6210\u7684Cookie\u7684\u5199\u5230\u5ba2\u6237\u7aef\u7684\u8def\u5f84\uff0c\u8eab\u4efd\u9a8c\u8bc1\u7968\u4e2d\u4fdd\u5b58\u8fd9\u4e2a\u8def\u5f84\u662f\u5728\u5237\u65b0\u8eab\u4efd\u9a8c\u8bc1\u7968Cookie\u65f6\u4f7f\u7528\uff08\u8fd9\u4e5f\u662f\u751f\u6210Cookie\u7684Path\uff09\uff0c\u82e5\u6ca1\u6709strCookiePath \u53c2\u6570\uff0c\u5219\u4f7f\u7528web.config\u4e2d path\u5c5e\u6027\u7684\u8bbe\u7f6e\u3002<\/p >\n<p >\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230,\u6b64\u65b9\u6cd5\u53c2\u6570\u53ea\u6709\u4e09\u4e2a,\u800c\u8eab\u4efd\u9a8c\u8bc1\u7968\u7684\u5c5e\u6027\u6709\u4e03\u4e2a,\u4e0d\u8db3\u7684\u56db\u4e2a\u53c2\u6570\u662f\u8fd9\u4e48\u6765\u7684:<br \/>IssueDate\uff1aCookie\u53d1\u51fa\u65f6\u95f4\u7531\u5f53\u524d\u65f6\u95f4\u5f97\u51fa,<br \/>Expiration\uff1a\u8fc7\u671f\u65f6\u95f4\u7531\u5f53\u524d\u65f6\u95f4\u548c&lt;forms&gt;\u6807\u7b7e\u4e2d\u7684timeout\u53c2\u6570\u7b97\u51fa\u3002\u6b64\u53c2\u6570\u5bf9\u975e\u6301\u4e45\u6027cookie\u6709\u610f\u4e49\u3002<br \/>UserData\uff1a\u8fd9\u4e2a\u5c5e\u6027\u53ef\u4ee5\u7528\u5e94\u7528\u7a0b\u5e8f\u5199\u5165\u4e00\u4e9b\u7528\u6237\u5b9a\u4e49\u7684\u6570\u636e,\u6b64\u65b9\u6cd5\u6ca1\u6709\u7528\u5230\u8fd9\u4e2a\u5c5e\u6027,\u53ea\u662f\u7b80\u5355\u7684\u5c06\u6b64\u5c5e\u6027\u7f6e\u4e3a\u7a7a\u5b57\u7b26\u4e32,\u8bf7\u6ce8\u610f\u6b64\u5c5e\u6027,\u5728\u540e\u9762\u6211\u4eec\u5c06\u8981\u4f7f\u7528\u5230\u8fd9\u4e2a\u5c5e\u6027\u3002<br \/>Version\uff1a \u7248\u672c\u53f7\u7531\u7cfb\u7edf\u81ea\u52a8\u63d0\u4f9b\u3002<\/p>\n<p>RedirectFromLoginPage\u65b9\u6cd5\u751f\u6210\u751f\u6210\u8eab\u4efd\u9a8c\u8bc1\u7968\u540e\uff0c\u4f1a\u8c03\u7528FormsAuthentication.Encrypt \u65b9\u6cd5\uff0c\u5c06\u8eab\u4efd\u9a8c\u8bc1\u7968\u52a0\u5bc6\u4e3a\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5c06\u4f1a\u662f\u4ee5.ASPXAUTH\u4e3a\u540d\u5b57\u7684\u4e00\u4e2aCookie\u7684\u503c\u3002<br \/>\u8fd9\u4e2aCookie\u7684\u5176\u5b83\u5c5e\u6027\u7684\u751f\u6210\uff1a<br \/>Domain\uff0cPath\u5c5e\u6027\u4e3a\u786e\u7701\u503c\uff0cExpires\u89c6createPersistentCookie\u53c2\u6570\u800c\u5b9a\uff0c\u82e5\u662f\u6301\u4e45cookie\uff0cExpires\u8bbe\u4e3a50\u5e74\u4ee5\u540e\u8fc7\u671f\uff1b\u82e5\u662f\u975e\u6301\u4e45cookie\uff0cExpires\u5c5e\u6027\u4e0d\u8bbe\u7f6e\u3002<br \/>\u751f\u6210\u8eab\u4efd\u9a8c\u8bc1Cookie\u540e\uff0c\u5c06\u6b64Cookie\u52a0\u5165\u5230Response.Cookies\u4e2d\uff0c\u7b49\u5f85\u53d1\u9001\u5230\u5ba2\u6237\u7aef\u3002<br \/>\u6700\u540eRedirectFromLoginPage\u65b9\u6cd5\u8c03\u7528FormsAuthentication.GetRedirectUrl \u65b9\u6cd5\u83b7\u53d6\u5230\u7528\u6237\u539f\u5148\u8bf7\u6c42\u7684\u9875\u9762\uff0c\u91cd\u5b9a\u5411\u5230\u8fd9\u4e2a\u9875\u9762\u3002<\/p >\n<p >3). &lt;forms&gt;\u6807\u7b7e\u4e2d\u7684timeout\u548cpath,\u662f\u63d0\u4f9b\u4e86\u8eab\u4efd\u9a8c\u8bc1\u7968\u5199\u5165\u5230Cookie\u8fc7\u671f\u65f6\u95f4\u548c\u9ed8\u8ba4\u8def\u5f84\u3002<\/p >\n<p >\u4ee5\u4e0a\u5c31\u662f\u57fa\u4e8eForms\u8eab\u4efd\u9a8c\u8bc1\u7684\u8fc7\u7a0b\uff0c\u5b83\u5b8c\u6210\u4e86\u5bf9\u7528\u6237\u8eab\u4efd\u7684\u786e\u8ba4\u3002<\/p >\n<p ><strong >2.\u5728\u53d7\u4fdd\u62a4\u7684\u6587\u4ef6\u5939\u5982Manage\u4e0b\u521b\u5efa\u4e00web.config\u6587\u4ef6,\u5185\u5bb9\u5982<\/strong ><\/p >\n<div style=\"BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; BORDER-BOTTOM-WIDTH: 1px; BORDER-BOTTOM-COLOR: #cccccc; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: bre\" ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >configuration<\/span ><span style=\"COLOR: #0000ff\" >&gt;<br \/><\/span ><span style=\"COLOR: #000000\" ><font color=\"#008000\" >&nbsp; &lt;!&#8211;\u6307\u5b9a\u5bf9\u6574\u4e2aManage\u76ee\u5f55\u7684\u8bbf\u95ee\u6743\u9650&#8211;&gt;<\/font ><br \/>&nbsp;&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >system<\/span ><span style=\"COLOR: #ff0000\" >.web<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;&nbsp;&nbsp;&nbsp; <\/span ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >authorization<\/span ><span style=\"COLOR: #0000ff\" >&gt;<br \/><\/span ><span style=\"COLOR: #000000\" ><span style=\"COLOR: #008000\" ><font color=\"#000000\" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<\/font >&lt;!&#8211;\u591a\u4e2a\u89d2\u8272\u7528,\u5206\u9694&#8211;&gt;<\/span ><span style=\"COLOR: #008000\" ><\/span ><br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >allow&nbsp;<\/span ><span style=\"COLOR: #ff0000\" >roles<\/span ><span style=\"COLOR: #0000ff\" >=&#8221;admin,user&#8221;<\/span ><span style=\"COLOR: #0000ff\" >\/&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<\/span ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >deny&nbsp;<\/span ><span style=\"COLOR: #ff0000\" >users<\/span ><span style=\"COLOR: #0000ff\" >=&#8221;*&#8221;<\/span ><span style=\"COLOR: #ff0000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >\/&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span ><span style=\"COLOR: #0000ff\" >&lt;\/<\/span ><span style=\"COLOR: #800000\" >authorization<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >&lt;\/<\/span ><span style=\"COLOR: #800000\" >system.web<\/span ><span style=\"COLOR: #0000ff\" >&gt;<br \/><\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;&nbsp;<\/span ><span style=\"COLOR: #008000\" >&lt;!&#8211;\u4e5f\u53ef<\/span ><span style=\"COLOR: #008000\" >\u63a7\u5236\u67d0\u4e2a\u9875\u7684\u6743\u9650<\/p>\n<p>&nbsp;&nbsp;&lt;location&nbsp;path=&#8221;AnnounceList.aspx&#8221;&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; &lt;system.web&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;authorization&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;allow&nbsp;roles=&#8221;admin&#8221;\/&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;deny&nbsp;users=&#8221;*&#8221;&nbsp;\/&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/authorization&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/system.web&gt;<br \/>&nbsp;&nbsp;&lt;\/location&gt;<\/p>\n<p>&nbsp;&nbsp;&lt;location&nbsp;path=&#8221;ConfigInfo.aspx&#8221;&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; &lt;system.web&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;authorization&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;allow&nbsp;roles=&#8221;users&#8221;\/&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;deny&nbsp;users=&#8221;*&#8221;&nbsp;\/&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/authorization&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/system.web&gt;<br \/>&nbsp;&nbsp;&lt;\/location&gt;<\/p>\n<p>&nbsp;&nbsp;<\/span ><span style=\"COLOR: #008000\" >&#8211;&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/><\/span ><span style=\"COLOR: #0000ff\" >&lt;\/<\/span ><span style=\"COLOR: #800000\" >configuration<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/><\/span ><\/div >\n<p >\u6ce8\uff1a\u6b64\u914d\u7f6e\u5185\u5bb9\u4e5f\u53ef\u4ee5\u52a0\u5165\u5230\u7cfb\u7edf\u7684web.config\u6587\u4ef6\u4e2d\uff0c\u6ce8\u610f\u52a0\u5165\u4f4d\u7f6e\uff1a<\/p >\n<div style=\"BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; BORDER-BOTTOM-WIDTH: 1px; BORDER-BOTTOM-COLOR: #cccccc; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: bre\" >\n<p dir=\"ltr\" style=\"MARGIN-RIGHT: 0px\" ><span style=\"COLOR: #000000\" >&#8230;&#8230;..<br \/><\/span ><span style=\"COLOR: #0000ff\" >&nbsp;&nbsp;&nbsp; &lt;\/<\/span ><span style=\"COLOR: #800000\" >system.web<\/span ><span style=\"COLOR: #0000ff\" >&gt;<br \/><\/span ><span style=\"COLOR: #000000\" ><br \/><\/span ><span style=\"COLOR: #0000ff\" ><span style=\"COLOR: #0000ff\" >&nbsp;&nbsp;&nbsp; <\/span >&lt;<\/span ><span style=\"COLOR: #800000\" >location&nbsp;<\/span ><span style=\"COLOR: #ff0000\" >path<\/span ><span style=\"COLOR: #0000ff\" >=&#8221;Manage\/AnnounceList.aspx&#8221;<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;<span style=\"COLOR: #0000ff\" >&nbsp;&nbsp;&nbsp; <\/span ><\/span ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >system<\/span ><span style=\"COLOR: #ff0000\" >.web<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;&nbsp;<span style=\"COLOR: #0000ff\" >&nbsp;&nbsp;&nbsp; <\/span ><\/span ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >authorization<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;&nbsp;&nbsp;<span style=\"COLOR: #0000ff\" >&nbsp;&nbsp;&nbsp; <\/span ><\/span ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >allow&nbsp;<\/span ><span style=\"COLOR: #ff0000\" >roles<\/span ><span style=\"COLOR: #0000ff\" >=&#8221;admin&#8221;<\/span ><span style=\"COLOR: #0000ff\" >\/&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;&nbsp;&nbsp;<span style=\"COLOR: #0000ff\" >&nbsp;&nbsp;&nbsp; <\/span ><\/span ><span style=\"COLOR: #0000ff\" >&lt;<\/span ><span style=\"COLOR: #800000\" >deny&nbsp;<\/span ><span style=\"COLOR: #ff0000\" >users<\/span ><span style=\"COLOR: #0000ff\" >=&#8221;*&#8221;<\/span ><span style=\"COLOR: #ff0000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >\/&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;&nbsp;<span style=\"COLOR: #0000ff\" >&nbsp;&nbsp;&nbsp; <\/span ><\/span ><span style=\"COLOR: #0000ff\" >&lt;\/<\/span ><span style=\"COLOR: #800000\" >authorization<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/><span style=\"COLOR: #0000ff\" >&nbsp;&nbsp;&nbsp;&nbsp;<\/span >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >&lt;\/<\/span ><span style=\"COLOR: #800000\" >system.web<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><span style=\"COLOR: #000000\" ><br \/><\/span ><span style=\"COLOR: #0000ff\" ><span style=\"COLOR: #0000ff\" >&nbsp;&nbsp;&nbsp; <\/span >&lt;\/<\/span ><span style=\"COLOR: #800000\" >location<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/p>\n<p><\/span ><span style=\"COLOR: #0000ff\" >&lt;\/<\/span ><span style=\"COLOR: #800000\" >configuration<\/span ><span style=\"COLOR: #0000ff\" >&gt;<\/span ><\/p ><\/div >\n<p >&lt;allow&gt;\u6807\u7b7e\u8868\u793a\u5141\u8bb8\u8bbf\u95ee\uff0c\u5176\u4e2d\u7684\u5c5e\u6027<br \/>1). users\uff1a\u4e00\u4e2a\u9017\u53f7\u5206\u9694\u7684\u7528\u6237\u540d\u5217\u8868\uff0c\u8fd9\u4e9b\u7528\u6237\u540d\u5df2\u88ab\u6388\u4e88\u5bf9\u8d44\u6e90\u7684\u8bbf\u95ee\u6743\u9650\u3002\u95ee\u53f7 (?) \u5141\u8bb8\u533f\u540d\u7528\u6237\uff1b\u661f\u53f7 (*) \u5141\u8bb8\u6240\u6709\u7528\u6237\u3002<br \/>2). roles\uff1a\u4e00\u4e2a\u9017\u53f7\u5206\u9694\u7684\u89d2\u8272\u5217\u8868\uff0c\u8fd9\u4e9b\u89d2\u8272\u5df2\u88ab\u6388\u4e88\u5bf9\u8d44\u6e90\u7684\u8bbf\u95ee\u6743\u9650\u3002<br \/>3). verbs\uff1a\u4e00\u4e2a\u9017\u53f7\u5206\u9694\u7684 HTTP \u4f20\u8f93\u65b9\u6cd5\u5217\u8868\uff0c\u8fd9\u4e9b HTTP \u4f20\u8f93\u65b9\u6cd5\u5df2\u88ab\u6388\u4e88\u5bf9\u8d44\u6e90\u7684\u8bbf\u95ee\u6743\u9650\u3002\u6ce8\u518c\u5230 ASP.NET \u7684\u8c13\u8bcd\u4e3a GET\u3001HEAD\u3001POST \u548c DEBUG\u3002<\/p >\n<p >&lt;deny&gt;\u6807\u7b7e\u8868\u793a\u4e0d\u5141\u8bb8\u8bbf\u95ee\u3002\u5176\u4e2d\u7684\u5c5e\u6027\u540c\u4e0a\u9762\u7684\u3002<\/p >\n<p >\u5728\u8fd0\u884c\u65f6\uff0c\u6388\u6743\u6a21\u5757\u8fed\u4ee3\u901a\u8fc7 &lt;allow&gt; \u548c &lt;deny&gt; \u6807\u8bb0\uff0c\u76f4\u5230\u5b83\u627e\u5230\u9002\u5408\u7279\u5b9a\u7528\u6237\u7684\u7b2c\u4e00\u4e2a\u8bbf\u95ee\u89c4\u5219\u3002\u7136\u540e\uff0c\u5b83\u6839\u636e\u627e\u5230\u7684\u7b2c\u4e00\u9879\u8bbf\u95ee\u89c4\u5219\u662f &lt;allow&gt; \u8fd8\u662f &lt;deny&gt; \u89c4\u5219\u6765\u5141\u8bb8\u6216\u62d2\u7edd\u5bf9 URL \u8d44\u6e90\u7684\u8bbf\u95ee\u3002Machine.config \u6587\u4ef6\u4e2d\u7684\u9ed8\u8ba4\u8eab\u4efd\u9a8c\u8bc1\u89c4\u5219\u662f &lt;allow users=&#8221;*&#8221;\/&gt;\uff0c\u56e0\u6b64\u9664\u975e\u53e6\u884c\u914d\u7f6e\uff0c\u5426\u5219\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u5141\u8bb8\u8bbf\u95ee\u3002<\/p>\n<p>\u90a3\u4e48\u8fd9\u4e9buser \u548croles\u53c8\u662f\u5982\u4f55\u5f97\u5230\u7684\u5462\uff1f\u4e0b\u9762\u770b\u4e00\u4e0b\u6388\u6743\u7684\u8be6\u7ec6\u8fc7\u7a0b\uff1a<\/p >\n<p >1). \u4e00\u65e6\u4e00\u4e2a\u7528\u6237\u8bbf\u95ee\u8fd9\u4e2a\u7f51\u7ad9\uff0c\u5c31\u884c\u767b\u5f55\u786e\u8ba4\u4e86\u8eab\u4efd\uff0c\u8eab\u4efd\u9a8c\u8bc1\u7968\u7684cookie\u4e5f\u5199\u5230\u4e86\u5ba2\u6237\u7aef\u3002\u4e4b\u540e\uff0c\u8fd9\u4e2a\u7528\u6237\u518d\u6b21\u7533\u8bf7\u8fd9\u4e2aweb\u7684\u9875\u9762\uff0c\u8eab\u4efd\u9a8c\u8bc1\u7968\u7684cookie\u5c31\u4f1a\u53d1\u9001\u5230\u670d\u52a1\u7aef\u3002\u5728\u670d\u52a1\u7aef\uff0casp.net\u4e3a\u6bcf\u4e00\u4e2ahttp\u8bf7\u6c42\u90fd\u5206\u914d\u4e00\u4e2aHttpApplication\u5bf9\u8c61\u6765\u5904\u7406\u8fd9\u4e2a\u8bf7\u6c42\uff0c\u5728HttpApplication.AuthenticateRequest\u4e8b\u4ef6\u540e\uff0c\u5b89\u5168\u6a21\u5757\u5df2\u5efa\u7acb\u7528\u6237\u6807\u8bc6\uff0c\u5c31\u662f\u6b64\u7528\u6237\u7684\u8eab\u4efd\u5728web\u7aef\u5df2\u7ecf\u5efa\u7acb\u8d77\u6765\uff0c\u8fd9\u4e2a\u8eab\u4efd\u5b8c\u5168\u662f\u7531\u5ba2\u6237\u7aef\u53d1\u9001\u56de\u6765\u7684\u8eab\u4efd\u9a8c\u8bc1\u7968\u7684cookie\u5efa\u7acb\u7684\u3002<br \/>2). \u7528\u6237\u8eab\u4efd\u5728HttpContext.User \u5c5e\u6027\u4e2d\uff0c\u5728\u9875\u9762\u4e2d\u53ef\u4ee5\u901a\u8fc7Page.Context \u6765\u83b7\u53d6\u540c\u8fd9\u4e2a\u9875\u9762\u76f8\u5173\u7684HttpContext\u5bf9\u8c61\u3002\u5bf9\u4e8eForms\u9a8c\u8bc1\uff0cHttpContext.User\u5c5e\u6027\u662f\u4e00\u4e2aGenericPrincipal\u7c7b\u578b\u7684\u5bf9\u8c61\uff0cGenericPrincipal\u53ea\u6709\u4e00\u4e2a\u516c\u5f00\u7684\u5c5e\u6027Identity\uff0c\u6709\u4e2a\u79c1\u6709\u7684m_role\u5c5e\u6027\uff0c\u662fstring[]\u7c7b\u578b\uff0c\u5b58\u653e\u6b64\u7528\u6237\u662f\u5c5e\u4e8e\u54ea\u4e9brole\u7684\u6570\u7ec4\uff0c\u8fd8\u6709\u4e00\u4e2a\u516c\u5f00\u7684\u65b9\u6cd5IsInRole(string role)\uff0c\u6765\u5224\u65ad\u6b64\u7528\u6237\u662f\u5426\u5c5e\u4e8e\u67d0\u4e2a\u89d2\u8272\u3002<br \/>\u7531\u4e8e\u8eab\u4efd\u9a8c\u8bc1\u7968\u7684cookie\u4e2d\u6839\u672c\u6ca1\u6709\u63d0\u4f9brole\u8fd9\u4e2a\u5c5e\u6027\uff0c\u5c31\u662f\u8bf4Forms\u8eab\u4efd\u9a8c\u8bc1\u7968\u6ca1\u6709\u63d0\u4f9b\u6b64\u7528\u6237\u7684role\u4fe1\u606f\uff0c\u6240\u4ee5\uff0c\u5bf9\u4e8eForms\u9a8c\u8bc1\uff0c\u5728\u670d\u52a1\u7aef\u5f97\u5230\u7684GenericPrincipal \u7528\u6237\u5bf9\u8c61\u7684m_role\u5c5e\u6027\u6c38\u8fdc\u662f\u7a7a\u7684\u3002<br \/>3). GenericPrincipal. Identity \u5c5e\u6027\u662f\u4e00\u4e2aFormsIdentity\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u6709\u4e2aName\u5c5e\u6027\uff0c\u5c31\u662f\u6b64\u7528\u6237\u7684\u6807\u793a\uff0c\u8bbf\u95ee\u6388\u6743\u5c31\u662f\u5c06\u6b64\u5c5e\u6027\u505a\u4e3auser\u6765\u8fdb\u884c\u6388\u6743\u9a8c\u8bc1\u7684\u3002FormsIdentity\u8fd8\u6709\u4e00\u4e2a\u5c5e\u6027\uff0c\u5c31\u662fTicket\u5c5e\u6027\uff0c\u6b64\u5c5e\u6027\u662f\u8eab\u4efd\u9a8c\u8bc1\u7968FormsAuthenticationTicket\u7c7b\u578b\uff0c\u5c31\u662f\u4e4b\u524d\u670d\u52a1\u5668\u5199\u5230\u5ba2\u6237\u7aef\u7684\u8eab\u4efd\u9a8c\u8bc1\u7968\u3002<br \/>\u670d\u52a1\u5668\u5728\u83b7\u53d6\u5230\u8eab\u4efd\u9a8c\u8bc1\u7968FormsAuthenticationTicket\u5bf9\u8c61\u540e\uff0c\u67e5\u770b\u8fd9\u4e2a\u8eab\u4efd\u9a8c\u8bc1\u7968\u662f\u4e0d\u662f\u975e\u6301\u4e45\u7684\u8eab\u4efd\u9a8c\u8bc1\uff0c\u662f\u7684\u8bdd\u8981\u6839\u636eweb.config\u4e2dtimeout\u5c5e\u6027\u8bbe\u7f6e\u7684\u6709\u6548\u671f\u6765\u66f4\u65b0\u8fd9\u4e2a\u8eab\u4efd\u9a8c\u8bc1\u7968\u7684cookie\uff08\u4e3a\u907f\u514d\u5371\u53ca\u6027\u80fd\uff0c\u5728\u7ecf\u8fc7\u4e86\u8d85\u8fc7\u4e00\u534a\u7684\u6307\u5b9a\u65f6\u95f4\u540e\u66f4\u65b0\u8be5 Cookie\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u7cbe\u786e\u6027\u4e0a\u7684\u635f\u5931\u3002\u6301\u4e45\u6027 Cookie \u4e0d\u8d85\u65f6\u3002\uff09<br \/>4). \u5728HttpApplication.ResolveRequestCache\u4e8b\u4ef6\u4e4b\u524d\uff0casp.net\u5f00\u59cb\u53d6\u5f97\u7528\u6237\u8bf7\u6c42\u7684\u9875\u9762\uff0c\u5efa\u7acbHttpHandler\u63a7\u5236\u70b9\u3002\u8fd9\u5c31\u610f\u5473\u7740\uff0c\u5728HttpApplication.ResolveRequestCache\u4e8b\u4ef6\u8981\u5bf9\u7528\u6237\u8bbf\u95ee\u6743\u9650\u5c31\u884c\u9a8c\u8bc1\uff0c\u770b\u6b64\u7528\u6237\u6216\u89d2\u8272\u662f\u5426\u6709\u6743\u9650\u8bbf\u95ee\u8fd9\u4e2a\u9875\u9762\uff0c\u4e4b\u540e\u5728\u8fd9\u4e2a\u8bf7\u6c42\u7684\u751f\u547d\u5468\u671f\u5185\u518d\u6539\u53d8\u6b64\u7528\u6237\u7684\u8eab\u4efd\u6216\u89d2\u8272\u5c31\u6ca1\u6709\u610f\u4e49\u4e86\u3002<\/p>\n<p>\u4ee5\u4e0a\u662fForms\u9a8c\u8bc1\u7684\u5168\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u770b\u51fa\uff0c\u8fd9\u4e2aForms\u9a8c\u8bc1\u662f\u57fa\u4e8e\u7528\u6237\u7684\uff0c\u6ca1\u6709\u4e3a\u89d2\u8272\u7684\u9a8c\u8bc1\u63d0\u4f9b\u76f4\u63a5\u652f\u6301\u3002\u8eab\u4efd\u9a8c\u8bc1\u7968FormsAuthenticationTicket \u4e2d\u7684Name\u5c5e\u6027\u662f\u7528\u6237\u6807\u793a\uff0c\u5176\u5b9e\u8fd8\u6709\u4e00\u4e2a\u5c5e\u6027UserData\uff0c\u8fd9\u4e2a\u5c5e\u6027\u53ef\u4ee5\u7531\u5e94\u7528\u7a0b\u5e8f\u6765\u5199\u5165\u81ea\u5b9a\u4e49\u7684\u4e00\u4e9b\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u5b57\u6bb5\u6765\u5b58\u653erole\u7684\u4fe1\u606f\uff0c\u4ece\u800c\u8fbe\u5230\u57fa\u4e8e\u89d2\u8272\u9a8c\u8bc1\u7684\u76ee\u7684\u3002<\/p >\n<p ><strong >3.\u767b\u5f55\u9875<\/strong ><\/p >\n<div style=\"BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; BORDER-BOTTOM-WIDTH: 1px; BORDER-BOTTOM-COLOR: #cccccc; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: bre\" ><span style=\"COLOR: #008000\" >\/\/<\/span ><span style=\"COLOR: #008000\" >\u767b\u5f55\u6309\u94ae<\/span ><span style=\"COLOR: #008000\" ><br \/><\/span ><span style=\"COLOR: #0000ff\" >private<\/span ><span style=\"COLOR: #000000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >void<\/span ><span style=\"COLOR: #000000\" >&nbsp;Button1_Click(<\/span ><span style=\"COLOR: #0000ff\" >object<\/span ><span style=\"COLOR: #000000\" >&nbsp;sender,&nbsp;System.EventArgs&nbsp;e)<br \/>{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"COLOR: #008000\" >\/\/\u5b9e\u4f53\u7c7bAdminUserVO\u5bf9\u5e94AdminUser\u7528\u6237\u8868\u3002<\/span ><span style=\"COLOR: #008000\" ><br \/><\/span >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AdminUserVO&nbsp;adminUserVO&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >new<\/span ><span style=\"COLOR: #000000\" >&nbsp;AdminUserVO();<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;adminUserVO.Uname&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;UserName.Text.Trim();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;adminUserVO.Upwd&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;UserPwd.Text.Trim();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;adminUserVO.LastIP&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;HttpContext.Current.Request.UserHostAddress;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;adminUserVO.LastTime&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;DateTime.Now;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >bool<\/span ><span style=\"COLOR: #000000\" >&nbsp;flag&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;(<\/span ><span style=\"COLOR: #0000ff\" >new<\/span ><span style=\"COLOR: #000000\" >&nbsp;LoginDAO()).Chk(adminUserVO);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >if<\/span ><span style=\"COLOR: #000000\" >&nbsp;(flag)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #008000\" >\/\/<\/span ><span style=\"COLOR: #008000\" >\u975e\u89d2\u8272\u9a8c\u8bc1\u65f6\u53ef\u4ee5\u7528\u8fd9\u53e5:<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #008000\" >\/\/<\/span ><span style=\"COLOR: #008000\" >System.Web.Security.FormsAuthentication.SetAuthCookie(UserName.Text.Trim(),false);<\/p>\n<p><font color=\"#000000\" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/font ><\/span ><span style=\"COLOR: #008000\" >\/\/\u521b\u5efa<\/span ><span style=\"COLOR: #008000\" >\u89d2\u8272\u9a8c\u8bc1\u4fe1\u606f,\u628arole\u4fe1\u606f\u5199\u5165\u5230UserData\u4e2d<\/span ><span style=\"COLOR: #008000\" ><br \/><\/span ><span style=\"COLOR: #000000\" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"COLOR: #ff0000\" >SetLoginCookie(adminUserVO,adminUserVO.Roles.ToLower());<br \/><\/span ><br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpContext.Current.Response.Redirect(<\/span ><span style=\"COLOR: #000000\" >&#8220;<\/span ><span style=\"COLOR: #000000\" >Main.aspx<\/span ><span style=\"COLOR: #000000\" >&#8220;<\/span ><span style=\"COLOR: #000000\" >);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >else<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpContext.Current.Response.Write(<\/span ><span style=\"COLOR: #000000\" >&#8220;<\/span ><span style=\"COLOR: #000000\" >\u767b\u5f55\u5931\u8d25<\/span ><span style=\"COLOR: #000000\" >&#8220;<\/span ><span style=\"COLOR: #000000\" >);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>}<br \/><\/span ><\/div >\n<p ><\/p >\n<div style=\"BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; BORDER-BOTTOM-WIDTH: 1px; BORDER-BOTTOM-COLOR: #cccccc; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: bre\" ><span style=\"COLOR: #008000\" >\/\/<\/span ><span style=\"COLOR: #008000\" >SetLoginCookie\u65b9\u6cd5<\/span ><span style=\"COLOR: #008000\" ><br \/><\/span ><span style=\"COLOR: #0000ff\" >public<\/span ><span style=\"COLOR: #000000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >static<\/span ><span style=\"COLOR: #000000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >void<\/span ><span style=\"COLOR: #000000\" >&nbsp;SetLoginCookie(AdminUserVO&nbsp;u,&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >string<\/span ><span style=\"COLOR: #000000\" >&nbsp;roles)<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #008000\" >\/\/<\/span ><span style=\"COLOR: #008000\" >\u5efa\u7acb\u8eab\u4efd\u9a8c\u8bc1\u7968\u5bf9\u8c61<\/span ><span style=\"COLOR: #008000\" ><br \/><\/span ><span style=\"COLOR: #000000\" >&nbsp;&nbsp;&nbsp;FormsAuthenticationTicket&nbsp;ticket&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >new<\/span ><span style=\"COLOR: #000000\" >&nbsp;FormsAuthenticationTicket&nbsp;(<\/span ><span style=\"COLOR: #000000\" >1<\/span ><span style=\"COLOR: #000000\" >,u.Uname,&nbsp;DateTime.Now,&nbsp;DateTime.Now.AddMinutes(<\/span ><span style=\"COLOR: #000000\" >30<\/span ><span style=\"COLOR: #000000\" >),&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >false<\/span ><span style=\"COLOR: #000000\" >,roles,<\/span ><span style=\"COLOR: #000000\" >&#8220;<\/span ><span style=\"COLOR: #000000\" >\/<\/span ><span style=\"COLOR: #000000\" >&#8220;<\/span ><span style=\"COLOR: #000000\" >);<br \/>&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #008000\" >\/\/<\/span ><span style=\"COLOR: #008000\" >\u52a0\u5bc6\u5e8f\u5217\u5316\u9a8c\u8bc1\u7968\u4e3a\u5b57\u7b26\u4e32<\/span ><span style=\"COLOR: #008000\" ><br \/><\/span ><span style=\"COLOR: #000000\" >&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >string<\/span ><span style=\"COLOR: #000000\" >&nbsp;hashTicket&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;FormsAuthentication.Encrypt&nbsp;(ticket)&nbsp;;<br \/>&nbsp;&nbsp;&nbsp;HttpCookie&nbsp;userCookie&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >new<\/span ><span style=\"COLOR: #000000\" >&nbsp;HttpCookie(FormsAuthentication.FormsCookieName,&nbsp;hashTicket);<br \/>&nbsp;&nbsp;&nbsp;HttpContext.Current.Response.Cookies.Add(userCookie);<br \/>&nbsp;&nbsp;}<br \/><\/span ><\/div >\n<p >FormsAuthenticationTicket\u53c2\u6570\u8bf4\u660e\uff1a<br \/>FormsAuthenticationTicket(<br \/>int version, <font color=\"#339900\" >\/\/\u8bbe\u4e3a1,\u7248\u672c\u53f7\u7531\u7cfb\u7edf\u81ea\u52a8\u63d0\u4f9b<\/font ><br \/>string name, <font color=\"#339900\" >\/\/\u7528\u6237\u6807\u793a,\u83b7\u53d6\u4e0e\u8eab\u4efd\u9a8c\u8bc1 Cookie \u5173\u8054\u7684\u7528\u6237\u540d<\/font ><br \/>DateTime issueDate, <font color=\"#339900\" >\/\/Cookie \u7684\u53d1\u51fa\u65f6\u95f4, \u8bbe\u7f6e\u4e3a DateTime.Now<\/font > <br \/>DateTime expiration, <font color=\"#339900\" >\/\/\u83b7\u53d6 Cookie \u8fc7\u671f\u7684\u65e5\u671f\/\u65f6\u95f4<\/font ><br \/>bool isPersistent, <font color=\"#339900\" >\/\/\u662f\u5426\u6301\u4e45\u6027(\u6839\u636e\u9700\u8981\u8bbe\u7f6e,\u82e5\u662f\u8bbe\u7f6e\u4e3a\u6301\u4e45\u6027,\u5728\u53d1\u51fa<\/font ><font color=\"#339900\" >cookie\u65f6,cookie\u7684Expires\u8bbe\u7f6e\u4e00\u5b9a\u8981\u8bbe\u7f6e),\u5982\u679c\u5df2\u53d1\u51fa\u6301\u4e45\u7684 Cookie\uff0c\u5219\u8fd4\u56de true\u3002\u5426\u5219\uff0c\u8eab\u4efd\u9a8c\u8bc1 Cookie \u5c06\u9650\u5236\u5728\u6d4f\u89c8\u5668\u751f\u547d\u5468\u671f\u8303\u56f4\u5185\u3002<\/font ><br \/>string userData, <font color=\"#339900\" >\/\/\u83b7\u53d6\u5b58\u50a8\u5728 Cookie \u4e2d\u7684\u5e94\u7528\u7a0b\u5e8f\u5b9a\u4e49\u5b57\u7b26\u4e32,\u8fd9\u91cc\u7528\u4e0a\u9762\u51c6\u5907\u597d\u7684\u7528\u9017\u53f7\u5206\u5272\u7684role\u5b57\u7b26\u4e32<\/font ><br \/>string cookiePath <font color=\"#339900\" >\/\/ \u8fd4\u56de\u53d1\u51fa Cookie \u7684\u8def\u5f84\u3002\u6ce8\u610f\uff0c\u7a97\u4f53\u7684\u8def\u5f84\u8bbe\u7f6e\u4e3a&#8221;\/&#8221;,\u8fd9\u8981\u540c\u53d1\u51facookie\u7684\u8def\u5f84\u4e00\u81f4,\u56e0\u4e3a\u5237\u65b0cookie\u8981\u7528\u8fd9\u4e2a\u8def\u5f84\u3002\u7531\u4e8e\u7a97\u4f53\u533a\u5206\u5927\u5c0f\u5199\uff0c\u8fd9\u662f\u4e3a\u4e86\u9632\u6b62\u7ad9\u70b9\u4e2d\u7684 URL \u7684\u5927\u5c0f\u5199\u4e0d\u4e00\u81f4\u800c\u91c7\u53d6\u7684\u4e00\u79cd\u4fdd\u62a4\u63aa\u65bd\u3002<br \/><\/font >);<\/p>\n<p><strong >4.Global.asax.cs<\/strong ><\/p >\n<div style=\"BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; BORDER-BOTTOM-WIDTH: 1px; BORDER-BOTTOM-COLOR: #cccccc; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: bre\" ><span style=\"COLOR: #0000ff\" >protected<\/span ><span style=\"COLOR: #000000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >void<\/span ><span style=\"COLOR: #000000\" >&nbsp;Application_AuthenticateRequest(Object&nbsp;sender,&nbsp;EventArgs&nbsp;e)<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;HttpApplication&nbsp;app&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;(HttpApplication)&nbsp;sender;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;HttpContext&nbsp;ctx&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;app.Context&nbsp;;&nbsp;<\/span ><span style=\"COLOR: #008000\" >\/\/<\/span ><span style=\"COLOR: #008000\" >\u83b7\u53d6\u672c\u6b21Http\u8bf7\u6c42\u7684HttpContext\u5bf9\u8c61&nbsp;&nbsp;<\/span ><span style=\"COLOR: #008000\" ><br \/><\/span ><span style=\"COLOR: #000000\" >&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >if<\/span ><span style=\"COLOR: #000000\" >&nbsp;(ctx.User&nbsp;<\/span ><span style=\"COLOR: #000000\" >!=<\/span ><span style=\"COLOR: #000000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >null<\/span ><span style=\"COLOR: #000000\" >)<br \/>&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >if<\/span ><span style=\"COLOR: #000000\" >&nbsp;(ctx.Request.IsAuthenticated&nbsp;<\/span ><span style=\"COLOR: #000000\" >==<\/span ><span style=\"COLOR: #000000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >true<\/span ><span style=\"COLOR: #000000\" >)&nbsp;<\/span ><span style=\"COLOR: #008000\" >\/\/<\/span ><span style=\"COLOR: #008000\" >\u9a8c\u8bc1\u8fc7\u7684\u4e00\u822c\u7528\u6237\u624d\u80fd\u8fdb\u884c\u89d2\u8272\u9a8c\u8bc1&nbsp;&nbsp;<\/span ><span style=\"COLOR: #008000\" ><br \/><\/span ><span style=\"COLOR: #000000\" >&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Web.Security.FormsIdentity&nbsp;fi&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;(System.Web.Security.FormsIdentity)ctx.User.Identity&nbsp;;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Web.Security.FormsAuthenticationTicket&nbsp;ticket&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;fi.Ticket&nbsp;;&nbsp;<\/span ><span style=\"COLOR: #008000\" >\/\/<\/span ><span style=\"COLOR: #008000\" >\u53d6\u5f97\u8eab\u4efd\u9a8c\u8bc1\u7968&nbsp;&nbsp;<\/span ><span style=\"COLOR: #008000\" ><br \/><\/span ><span style=\"COLOR: #000000\" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >string<\/span ><span style=\"COLOR: #000000\" >&nbsp;userData&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;ticket.UserData;<span style=\"COLOR: #008000\" >\/\/\u4eceUserData\u4e2d\u6062\u590drole\u4fe1\u606f<\/span ><br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >string<\/span ><span style=\"COLOR: #000000\" >[]&nbsp;roles&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;userData.Split&nbsp;(<\/span ><span style=\"COLOR: #000000\" >&#8216;<\/span ><span style=\"COLOR: #000000\" >,<\/span ><span style=\"COLOR: #000000\" >&#8216;<\/span ><span style=\"COLOR: #000000\" >)&nbsp;;&nbsp;<\/span ><span style=\"COLOR: #008000\" >\/\/<\/span ><span style=\"COLOR: #008000\" >\u5c06\u89d2\u8272\u6570\u636e\u8f6c\u6210\u5b57\u7b26\u4e32\u6570\u7ec4,\u5f97\u5230\u76f8\u5173\u7684\u89d2\u8272\u4fe1\u606f&nbsp;<\/span ><span style=\"COLOR: #000000\" >&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctx.User&nbsp;<\/span ><span style=\"COLOR: #000000\" >=<\/span ><span style=\"COLOR: #000000\" >&nbsp;<\/span ><span style=\"COLOR: #0000ff\" >new<\/span ><span style=\"COLOR: #000000\" >&nbsp;System.Security.Principal.GenericPrincipal&nbsp;(fi,&nbsp;roles)&nbsp;;&nbsp;<\/span ><span style=\"COLOR: #008000\" >\/\/<\/span ><span style=\"COLOR: #008000\" >\u8fd9\u6837\u5f53\u524d\u7528\u6237\u5c31\u62e5\u6709\u89d2\u8272\u4fe1\u606f\u4e86<\/span ><span style=\"COLOR: #000000\" ><br \/>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br \/>&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;}<\/span ><\/div >\n<p >\u6ce8:\u5982\u679c\u4f7f\u7528HttpModule\u7684\u8bdd,\u6b64\u5904\u4ee3\u7801\u5e94\u8be5\u52a0\u5165\u5728AuthenticateRequest\u4e8b\u4ef6\u4e2d\u3002<\/p >\n<p >\u8f6c\u81ea\uff1a<a href=\"http:\/\/www.cnblogs.com\/yao\/archive\/2006\/06\/24\/434783.html\" >http:\/\/www.cnblogs.com\/yao\/archive\/2006\/06\/24\/434783.html<\/a ><\/p ><\/div ><\/div>\n","protected":false},"excerpt":{"rendered":"<p>asp.net\u4e2d\u4f7f\u7528\u57fa\u4e8e\u89d2\u8272role\u7684Forms\u9a8c\u8bc1\uff0c\u5927\u81f4\u7ecf\u8fc7\u51e0\u4e0b\u56db\u6b65\uff1a1.\u914d\u7f6e\u7cfb\u7edfweb.config &#038;l [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-469","post","type-post","status-publish","format-standard","hentry","category-all"],"views":1681,"_links":{"self":[{"href":"https:\/\/niuduer.sincebirth.space\/index.php?rest_route=\/wp\/v2\/posts\/469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/niuduer.sincebirth.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/niuduer.sincebirth.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/niuduer.sincebirth.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/niuduer.sincebirth.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=469"}],"version-history":[{"count":0,"href":"https:\/\/niuduer.sincebirth.space\/index.php?rest_route=\/wp\/v2\/posts\/469\/revisions"}],"wp:attachment":[{"href":"https:\/\/niuduer.sincebirth.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/niuduer.sincebirth.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/niuduer.sincebirth.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}