八、部署應用
VS7將一個project編譯成一個DLL文件,是一個NGWS裝配,一個裝配可以用在一臺機器上,放到 全局Cache,供所有的應用訪問,也可以放到某個應用的裝配Cache中,只讓一個應用訪問。 ASP+允許動態地引用一個類,只需要提供其裝配和類名,格式: assemlyname#classname
九、安全
1).認證和授權 除了利用Windows提供的認證服務外,ASP+還提供了一種服務,使基于 FORM的認證很方便地實現。 它是基于COOKIE工作的,所以客戶瀏覽器必須支持COOKIE。需要注意的是,ASP+的認證服務是從屬于 IIS的認證服務的。 ASP+提供兩種類型的認證服務,一是基于文件的ACL的,另一種是基于URL的。基于URL的培植在 配置文件中。 配置<security>節中的<authentication>元素,可以有下面幾種值: none:沒有認證 Windows:NT的用戶/組 Cookie:將未認證的用戶轉向一個特定的登陸頁面。最常用的一種方法。 Passport:必須安裝PassPort服務。
例: <configuration> <security> <authentication mode="Cookie"/> </security> </configuration>
2).基于Windows的認證 當采用它時,一個WindowsPrincipal對象被附加到Request對象中。程序中可以判定當前用戶是否 某類角色,如: if(User.IsInrole("Administrators")) ...... 還可以取得用戶名,如: User.Identity.Name; User.Identity.Type;
3).基于FORM的認證 a).選cookie模式,禁止匿名訪問: <authentication mode="Cookie"/> <authorization> <deny users="?"/> </authorization> b).配置登陸頁,加密用的鍵,以及Cookie名字,在<authentication>的子元素中: <cookie decryptionkey="autogenerate" loginurl="login.aspx" cookie=".ASPXCOOKIESDEMO"/> 注意,loginurl可以是遠程機器,但decryptionkey則在兩臺機器上的必須相同。descryptionkey 設成autogenerate則ASP+自動選擇。但如果一臺機器上有多個應用,則最好指定。同時,不同的應用 也該用不同的Cookie名字。因為同一臺機器上的所有應用設置的Cookie都將被客戶傳回來,所以不能同名。 c).提供登陸頁 d).驗證完后(用你自己的驗證機制,比如同數據庫中的記錄做比較),用下面一行: CookieAuthentication.RedirectFromLoginPage(username,persistence) 返回登陸頁前一頁。 這一句也設定了Cookie,從而讓它之后的ASP+認證服務認為用戶已經經過了認證。 如果不想轉向原來的頁,而是出現特定的頁,比如登陸用戶可選菜單頁,那必須使用另外的方法, 可以用CookieAuthentication.SetAuthCookie設置好Cookie,用CookieAutentication.GetAuthCookie 來獲得Cookie. 另外,上面那行中的第二個參數是一個bool值,表示是否讓Cookie永久保存,如果為false的話, 則當用戶關閉瀏覽器后,cookie就消失了。 用CookieAuthentication.SignOut可以清除Cookie,對應用戶退出登陸。 除了以上的用程序自己實現認證過程外,也可以用配置文件來實現讓ASP+幫你完成驗證。在 <authentication>節中: <credentials passwordformat="SHA1"> <user name="white" password="ASPFSSA98527357"> </credentials> 然后程序調用CookieAuthenticationManager.Authenticate,提供用戶名和口令作為參數,就可以 由ASP+幫你判定用戶是否合法了。 加密算法支持 Clear,SHA1,MD5。
4).認證擁護的角色 可以針對用戶,也可以針對角色(組),如: <authorization> <allow users="towhite@263.net"/> <allow roles="Admins"/> <deny users="*"/>
多個用戶名間用逗號","分割。 還可以細化請求方法: <allow verb="post" users="white,saillor"/> 其中,*代表任何人,?代表匿名用戶。
十、國際化,本地化應用
ASP+內部使用UNICODE,NGWS內部基類的String也是用UNICODE。可以支持某種特定的編碼,實現轉換。 場所屬性可以通過CultureInfo類訪問,其中,CurrentCulture是同場所有關的函數的缺省值, 而CurrentUICulture是場所上的資源數據格式,例: <%=CultureInfo.CurrentCulture.NativeName %> <%=CultureInfo.currentUICulture.NativeName %> 一些與場所有關的類提供格式化輸出,如: <%=DateTime.Now.Format("f",null)%> <%=DateTime.Now.Format("f",new System.Globalization.CultureInfo("de"))%>
可以為某個目錄進行配置,如: <configuration> <globalization fileencoding = "utf-8" requestencoding = "utf-8" responseencoding = "utf-8" culture = "en-us" uiculture = "de" /> 或者在Page指令中: <%@ Page Culture ="fr" UICulture = "fr" ResponseEncoding = "utf-8" %> 在頁面內部還可以隨時更改,使用 Thread.CurrentCulture修改,也就是說,同一個頁面可以使用 很多種編碼輸出。
1).設置文化和編碼 中文的Culture應設為:zh-cn CultureInfo.CurrentCulture.Name = "zh-cn"; CultureInfo.CurrentCulture.EnglishName = "Chinese(Peoples' Republic of China"; CultureInfo.CurrentCulture.NativeName = "中文(簡體)(中華人民共和國)"; 另外,類RegionInfo還提供地域信息: RegionInfo.CurrentRegion.NativeName = "中華人民共和國"; RegionInfo.CurrentRegion.CurrencySymbol = "¥";
2).本地化ASP+應用 3).使用資源文件 NGWS基類支持,運行時有個類叫ResourceManager的實例可以使用。可以用ResourceWriter或者實用 工具resgen.exe來生成資源文件,resgen以 key = value 的形式作為輸入,如: ; ;注釋 ; [Strings] greetings = 歡迎你! more = 更多新聞
資源文件的后綴為.resources。
如何在頁面中使用資源文件? 用戶的Content-Language可以用Request.UserLanguages[0]來取得。 如何實現多語言支持? a).準備資源文件,生成.resources文件,文件取名規則:中間帶Culture名。例:articles.en-us.resources b).global.asax中取得一個ResourceManager,并放如Application中供整個Application使用 c).global.asax中為Application_BeginRequest事件寫代碼,根據客戶的情況決定當前的Culture. d).在頁面中用ResourceManager.GetString取得內容。
例: //global.asax中: void Application_OnStart(){ Application["RM"]=new ResourceManager("articles",Server.Mappath("resources") +Enviroment.DirectorySeparatorChar,null); } void Application_BeginRequest(Object sender,EventArgs e){ try { Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.UserLanguages[0]); }catch(ArgumentException){ Thread.CurrentThread.CurrentCulture=new CultureInfo("en-us"); } Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; }
//default.asax中: ResourceManager rm; void Page_Init(Object sender,EventArgs e){ rm=(ResouceManager)Application["RM"]; }
//輸出內容時: <%= rm.GetString("greetings") %>
|