SecureString的特性很迷人,在記憶體裡是加密的字串,可以Dispose掉,不會在CLR下很輕易的被反組譯。
詳細特性可以看http://msdn2.microsoft.com/en-us/library/7kt014s1.aspx,或是在MSDN裡找到。在應用上的特點你可以看MSDN Magazine裡的資料,http://msdn.microsoft.com/msdnmag/issues/06/00/SecurityBriefs/default.aspx,裡面提到的安全性加強真是令人血脈僨張。
不過,當你看到MSDN也好,MSDN Maganize也好,都沒有相關的範例,怎麼辦。好吧!向Google大神求助吧!
http://blogs.msdn.com/shawnfa/archive/2004/05/27/143254.aspx
shawnfa的Blog算是最清晰的一個了,有教你怎麼把該加密的字串送進去,有教你怎麼把他從記憶體裡清掉。
但是那怎麼把字串弄出來送到Connection.ConnectionString呢?
這裡就是我試出來的答案,不過還是有疑慮,那ConnectionString安全嗎?在轉出到ConnectionString的期間,安全嗎?我不知道,這兩天會找找答案在哪裡。
//Insert String into SecureString in Constructor, I dont try this
SecureString password = new SecureString(char[],length);
//Append String into SecureString
foreach(char c in s) {
SecureString.AppendChar(c);
}
//GetString, Apply, And Dispose
void RunXX(){
IntPtr ptr = Marshal.SecureStringToBSTR(SecureConnectionString);(*1)
try {
SqlConnection conn = new SqlConnection();
conn.ConnectionString = Marshal.PtrToStringBSTR(ptr);(*2)
conn.Open();
... Run Query or something....
} finally {
Marshal.ZeroFreeBSTR(ptr);(*3)
}
}
以下是我的想法啦,畢竟對於Unmanage Code的使用真的不太熟,請盡量糾正錯誤,不要客氣。
*1: 這裡算是把SecureString的資料解到一個Unmanage的指標下。
*2: 把Unmanage的字串解到Connection的ConnectionString裡,就是這裡有安全性疑慮。
*3: 最後要把放在Unmanage的指標字串清掉,就沒人能反組譯到了。
3.29.2006
2.16.2006
.NET 2.0 And Configuration 2/4
接下來我們來看針對使用者範疇的三種狀況下,我們的使用者所選定的配色資料到底會跑到哪裡去。
在.NET 2.0 裡面,檔案會放在這個路徑下
\\__\\user.config
我們就解釋其中幾項代表的意義,之後我們會來弄個實作來驗證Configuration System。
首先是,在Windows XP裡,指的就是C:\Documents and Settings下你所登入的User名字的資料匣,當然啦,如果你的系統碟不在C,或是Documents And Setting資料匣路徑有改過,你就要找你所設定的位置。 在你的應用程式裡的這個AssemblyCompanyAttribute,會帶到這個地方。__ 這個就要看你的程式了。 就是程式的版本,這個後續我們會討論到版本變動的問題。
這個路徑不能改變!除非你自己實作自己的SettingProvider,SDK裡面有相關的資訊。
那為甚麼這個路徑不能改變呢?Prabhu並沒有解釋很多,不過這裡倒是有一個面向可以考慮一下。
假設現在公司裡有架AD環境,使用者可以在任何電腦裡面登入,但都會取用他本身所屬的檔案與設定,像我的文件阿!我的最愛之類。那是因為公司裡有一台AD伺服器,會做這些設定的管理。現在問題來了,公司裡的每台電腦的設定與作業系統不一定會相同,當Tom的資料在他本機裡面是放在C:\Documents And Setting\Tom裡面,但是當跑到秘書Emily的電腦時,路徑卻變成D:\WINNT\Profile\Tom...難怪路徑會是固定的形式。至少現階段我打EA Sport的電動,它們的設定檔路徑就是設成這個格式的。
所以就這個面向說來,這應該是作業系統架構的概念上的問題,並不是.NET 2.0 的 Configuration System的問題。不過關於上述的Roaming profile的東西,有一篇關於它的好文章,倒是一定要看一下。http://blogs.msdn.com/oldnewthing/archive/2005/06/30/434209.aspx
在.NET 2.0 裡面,檔案會放在這個路徑下
我們就解釋其中幾項代表的意義,之後我們會來弄個實作來驗證Configuration System。
首先是
這個路徑不能改變!除非你自己實作自己的SettingProvider,SDK裡面有相關的資訊。
那為甚麼這個路徑不能改變呢?Prabhu並沒有解釋很多,不過這裡倒是有一個面向可以考慮一下。
假設現在公司裡有架AD環境,使用者可以在任何電腦裡面登入,但都會取用他本身所屬的檔案與設定,像我的文件阿!我的最愛之類。那是因為公司裡有一台AD伺服器,會做這些設定的管理。現在問題來了,公司裡的每台電腦的設定與作業系統不一定會相同,當Tom的資料在他本機裡面是放在C:\Documents And Setting\Tom裡面,但是當跑到秘書Emily的電腦時,路徑卻變成D:\WINNT\Profile\Tom...難怪路徑會是固定的形式。至少現階段我打EA Sport的電動,它們的設定檔路徑就是設成這個格式的。
所以就這個面向說來,這應該是作業系統架構的概念上的問題,並不是.NET 2.0 的 Configuration System的問題。不過關於上述的Roaming profile的東西,有一篇關於它的好文章,倒是一定要看一下。http://blogs.msdn.com/oldnewthing/archive/2005/06/30/434209.aspx
2.06.2006
.NET 2.0 And Configuration 1/4
在.Net Framework的時候我是從來沒用過Configuration,一直到了Enterprise Library出來,我才開始使用。就算到了現在,我對Configuration還是不輕易使用,畢竟我還是覺得IO的動作能少就少,講到這個,我還撥不出空去看是不是.Net 2.0之後,Configuration是不是有Cache的動作。
不過老實講,Configuration是有好處的,至少不用透過複雜又難搞的Register來做動作,就只要放在確定的地方,在某種層面來看,Configuration的出現,算是對.Net 宣稱的XCOPY部署作一個良性的相對應。雖然有點馬後炮,不過要是用過Register應該或多或少被搞到一陣子。
Configuration在.NET 2.0做了一些個改變,可以把它想做是把Enterprise Library的Configuration Block吸收進來,然後做了些改進。
之前的Configuration分成Machine.config與ApplicationName.exe.config兩塊,現在分的比較有架構的感覺(不過也有點制式)。
舉個例子來說,現在的Configuration分成兩個部份,假設我們建立了一個程式,這個程式呢,有一些設定可以由使用者變動的,假設說是應用程式的配色方式,當然啦,我們也會有一個預設的設定值。現在我們考慮將這些設定值交給Configuration來儲存使用,首先,Configuration可以分成兩部份,一部分是所謂的應用程式的範疇,另一部份就是使用者的範疇。簡單的說,應用程式的範疇就是預設值,就是應用程式出廠的設定值,可以放在Config檔一起出廠;而使用者的範疇就是使用者自己設定自己喜歡的配色後,所儲存的設定檔。
因為預設值是不給使用者改變的,也就是說在Runtime是不能由應用程式作改變的,所以它是唯讀的。
那使用者的範疇又劃分成none,PerUserRomaing,PerUserRoamingAnaLocal,第一個就是不管他,交給API去處理,後面兩個就是以前.NET 1.0、.NET 1.1沒有的東西了。由字面上來看我們可以知道PerUserRoaming指的是漫遊使用者,第二個就泛指兩者;如果你的Application會在AD的環境下使用,卻對漫遊使用者完全沒頭緒的話,請先研究一下AD。不過我通常會把第二個當作Local User。
不過老實講,Configuration是有好處的,至少不用透過複雜又難搞的Register來做動作,就只要放在確定的地方,在某種層面來看,Configuration的出現,算是對.Net 宣稱的XCOPY部署作一個良性的相對應。雖然有點馬後炮,不過要是用過Register應該或多或少被搞到一陣子。
Configuration在.NET 2.0做了一些個改變,可以把它想做是把Enterprise Library的Configuration Block吸收進來,然後做了些改進。
之前的Configuration分成Machine.config與ApplicationName.exe.config兩塊,現在分的比較有架構的感覺(不過也有點制式)。
舉個例子來說,現在的Configuration分成兩個部份,假設我們建立了一個程式,這個程式呢,有一些設定可以由使用者變動的,假設說是應用程式的配色方式,當然啦,我們也會有一個預設的設定值。現在我們考慮將這些設定值交給Configuration來儲存使用,首先,Configuration可以分成兩部份,一部分是所謂的應用程式的範疇,另一部份就是使用者的範疇。簡單的說,應用程式的範疇就是預設值,就是應用程式出廠的設定值,可以放在Config檔一起出廠;而使用者的範疇就是使用者自己設定自己喜歡的配色後,所儲存的設定檔。
因為預設值是不給使用者改變的,也就是說在Runtime是不能由應用程式作改變的,所以它是唯讀的。
那使用者的範疇又劃分成none,PerUserRomaing,PerUserRoamingAnaLocal,第一個就是不管他,交給API去處理,後面兩個就是以前.NET 1.0、.NET 1.1沒有的東西了。由字面上來看我們可以知道PerUserRoaming指的是漫遊使用者,第二個就泛指兩者;如果你的Application會在AD的環境下使用,卻對漫遊使用者完全沒頭緒的話,請先研究一下AD。不過我通常會把第二個當作Local User。
1.16.2006
上閤屋的青菜
忙死了!這一陣子公司在報客戶的信託所得稅,加上原有要做的工作,忙翻了,都快變成月休四日了!
家裡的電腦也跟我鬧脾氣,老是有小問題。手上有一份關於Configuration的資料也一直都沒空整理...
昨天,公司尾牙,去上閤屋吃。一桌兩個夾子,可以選炸的,炒的...等等之類的東西。但是很久才會來,大概半小時左右。
這張圖片就是,當夾子選了炒青菜後,過了半個小時後送了的。重點是,大家的筷子都還沒動,他一來就是這個樣子,-_-'''
上閤屋限時兩個小時吃飯,那我不就只能用兩個小時湊到勉強一盤的青菜,ㄟ...這個會不會太扯。
以前上閤屋旗艦店開張沒多久,我就有去交關(台語,消費的意思)過,那時候品質很優,蝦子新鮮送菜快,昨天吃尾牙沒抽到大獎就很悶了,蝦子不是很鮮也算了,那青菜真的就太無敵了!
------------------------------------------------------------------------------------
Here are practice for English writing, welcome any comment for grammar or tech.
I have been on the go all month!
11.14.2005
Remoting and Generic
這兩天試了試Remoting,發現之前有關於Generic的部份要加以修改,我想他是可以使用的,然而卻有部分的限制。
這兩天都在試動態Load Assembly,然後掛到Service上面,是的,可以做,也運作的非常好。不過之前測試的版本卻不行,這兩天,我想要找出來哪裡有錯。
另外有關於Remote Object的解構式,他比較特別的是,Server端的GC並不會去呼叫。
我本來的想法是把一個Singleton Remote Object當作一個Cache,在Constructor時讀取資料庫,然後所以的資料變動都放在他身上,然後當Client沒有動作超過五分鐘(Singleton Object Default Life Time),會呼叫解構式,那時候再寫進Database,不過,測試的結果似乎是GC並不會去Run這一段,Remote Object就直接消滅了。
修改:有關上面這一段,我有再做一個測試,就是在解構式裡面寫檔案,當然,當五分鐘過後再開一個新的Client去呼叫,會起一個新的Remote Object, 但是當Remote Service Close掉時,卻會呼叫這個Remote Object的解構式。所以上面那一段的說法有問題,應該是說GC會去處理,但是處理後並不一定會馬上消滅這個物件,消滅這個物件的準確時間看來部是我們可以知道的,所以不管怎麼說,把寫入資料庫的動作放在解構式看來很不智,呵呵。
這兩天要測試當List 或是 SortedList 包含可序列化的 Complex Object,是否可以使用在Remoting上面。
還有就是兩個版本的Load Assembly,為啥一個能跑,另一個不能跑。
--------------------------------------------------------------------
Here are practice for English writing, welcome any comment for grammar or tech.
I had test Remoting and Generic this week, and find out the way to run, there are some limited when using generic in remoting. So…the posted article is wrong.
I also test the way to dynamically load assembly and plug into remoting service. Indeed, It's work, the better thing is so smooth. I need to find the difference between the versions, because I spent a lot to turn the first version and cannot run till.
Beside that, I find the special thing about destructor of remote object. The special thing is that GC in the server side would not to call destructor when the object disposed.
The original blueprint of my idea is using singleton remote object as cache for some usual table in database. The remote object load data from database in constructor and whole change are imply in the object. Than, when five mins gone and no client call it, the object would to write data into database in destructor (the default life time for singleton object are five mins without any client call). But the result of this test is that seems GC would not call the object's destructor and dispose it directly.
Fix: I run another test after post this. The test is put an write file code in the remote object's destructor and wait the lift time. After five mins pass and I run a new client to call the remote object, the data show the object is new because the data had input was gone. But the write file code run in the time I close the service. The conclusion is the GC in server side would mark the disposed object but do not clear it immediately, the time point would be control by GC and we had no way to know. Whatever, put the code that write change into database into destructor is silly, ha.
Ok, next week I would test that is remoting could run with the generic container that contain the complex object. And the version difference.
這兩天都在試動態Load Assembly,然後掛到Service上面,是的,可以做,也運作的非常好。不過之前測試的版本卻不行,這兩天,我想要找出來哪裡有錯。
另外有關於Remote Object的解構式,他比較特別的是,Server端的GC並不會去呼叫。
我本來的想法是把一個Singleton Remote Object當作一個Cache,在Constructor時讀取資料庫,然後所以的資料變動都放在他身上,然後當Client沒有動作超過五分鐘(Singleton Object Default Life Time),會呼叫解構式,那時候再寫進Database,不過,測試的結果似乎是GC並不會去Run這一段,Remote Object就直接消滅了。
修改:有關上面這一段,我有再做一個測試,就是在解構式裡面寫檔案,當然,當五分鐘過後再開一個新的Client去呼叫,會起一個新的Remote Object, 但是當Remote Service Close掉時,卻會呼叫這個Remote Object的解構式。所以上面那一段的說法有問題,應該是說GC會去處理,但是處理後並不一定會馬上消滅這個物件,消滅這個物件的準確時間看來部是我們可以知道的,所以不管怎麼說,把寫入資料庫的動作放在解構式看來很不智,呵呵。
這兩天要測試當List 或是 SortedList 包含可序列化的 Complex Object,是否可以使用在Remoting上面。
還有就是兩個版本的Load Assembly,為啥一個能跑,另一個不能跑。
--------------------------------------------------------------------
Here are practice for English writing, welcome any comment for grammar or tech.
I had test Remoting and Generic this week, and find out the way to run, there are some limited when using generic in remoting. So…the posted article is wrong.
I also test the way to dynamically load assembly and plug into remoting service. Indeed, It's work, the better thing is so smooth. I need to find the difference between the versions, because I spent a lot to turn the first version and cannot run till.
Beside that, I find the special thing about destructor of remote object. The special thing is that GC in the server side would not to call destructor when the object disposed.
The original blueprint of my idea is using singleton remote object as cache for some usual table in database. The remote object load data from database in constructor and whole change are imply in the object. Than, when five mins gone and no client call it, the object would to write data into database in destructor (the default life time for singleton object are five mins without any client call). But the result of this test is that seems GC would not call the object's destructor and dispose it directly.
Fix: I run another test after post this. The test is put an write file code in the remote object's destructor and wait the lift time. After five mins pass and I run a new client to call the remote object, the data show the object is new because the data had input was gone. But the write file code run in the time I close the service. The conclusion is the GC in server side would mark the disposed object but do not clear it immediately, the time point would be control by GC and we had no way to know. Whatever, put the code that write change into database into destructor is silly, ha.
Ok, next week I would test that is remoting could run with the generic container that contain the complex object. And the version difference.
11.02.2005
Package Load Failure - RecipeManagerPackage
這個問題自從安裝了VSTS RC(Release Candidate)版就會發生,當安裝完一打開,就會跳出像圖片的這個錯誤訊息,選Yes,以後就不會出現,選No...他就會一直煩你。因為最近很忙,所以這兩天Visual Studio 2005 Final 出來了,uninstall VSTS RC後,再安裝VS 2005 Final. 又還是有這個錯。
很忙是因為公司裡要生個東西出來,Push的很兇。不過呢,你也知道,眾人之事,就是只要有一兩很扯的人,就會搞的整團亂。
今天才跑上去Feedback去查了查...唉
事情是這樣的,當你一打開Visaul Studio 2005,你會發現跳出一個錯誤,就是說沒辦法把"'Microsoft.Practices.RecipeFramework.VisualStudio.RecipeManagerPackage"這個東西Load上去之類的。
Feedback裡面的相關Post(都是Package Load Failure),我依照時間列在下面英文版裡面,看來問題未解決,不過呢,個人是比較希望能有個說明啦,因為不知道那是啥東西,也不給Fix...這很難令人心安
-----------------------------------------------------------------------------------------
Here are writting practice for English, welcome the comment for grammar.
The error message would happen after install VSTS RC(Visual Studio Team System Release Candicate). The error message as the picture would popup when you open IDE, choice Yes, then would not show again, else, it would be popup again and again.
I didnt try to fix it because busy. So, after Visual Studio 2205 released, I do uninstall VSTS RC and install Visual Studio 2005, the error still happen.
Because my division are doing all we can do in a new web application hardly. but, You know that, there are only need one or two special guy, the team work would be fall into marsh.
so...I check it today
The exact description is there are failure to load "'Microsoft.Practices.RecipeFramework.VisualStudio.RecipeManagerPackage" or something like this when you open Visual Studio or open(include create) the project.
Listed below is the relative post in feedback center, there are order by time.
Seems the problem do not resolve till.But, I thought that would be better to explain what is the package before the fix.
Package Load Failure
Opened Date: 2005-04-12 07:22:50 http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=af9f4001-b8b9-4990-bf42-3a016f5a5fa0
Load failure
Opened Date: 2005-04-19 09:35:24 http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=d4123792-a7fd-463f-9bba-01826edaa805
Package Load Failure for Package "...VSDesignPackage"
Opened Date: 2004-07-23 08:11:13 http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=3640d6a6-c789-4890-91f6-804cc2c81eb6
Package Load Failure - Release Candidate
Opened Date: 2005-10-06 14:54:04 http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=ba5d9e57-6aaa-4349-a1e5-7ea49408101a
Package Load Failure in RTM build 50727.26
Opened Date: 2005-10-24 07:23:01 http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=d6568c6c-2b79-416b-939f-a6f39eafa20b
11.01.2005
The Links For Pre Post
當在反序列化時會有BinaryFormatter失敗http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=b08b64df-49f2-44a3-ac10-7811281eb149
請注意最後三個Post。
特別是"Resolved as Postponed by Microsoft on 2005-09-26 at 13:02:38 "這一個。
下面是另一個類似的Feedback
BinaryFormatter.AssemblyFormat似乎沒有功能
http://lab.msdn.microsoft.com/productfeedback/ViewFeedback.aspx?FeedbackID=c2045aff-b884-4052-bbfe-3c8c87361396
最後是這個
錯誤訊息=BadImageFormatException:Not to possible to perform a call with reflection on a generic method on a .NET remoting transparent proxy
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=059c20b0-f9b5-4982-a822-9c41fcc5efdf
以上我沒去找相對映的中文錯誤訊息,有翻的都是按自己的意思翻,看英文比較準
----------------------------------------------------------------
Here are writting practice verion, welcome for comment.
BinaryFormatter fails to deserialize
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=b08b64df-49f2-44a3-ac10-7811281eb149
Please Focus on Last Tree Comments.
Especially "Resolved as Postponed by Microsoft on 2005-09-26 at 13:02:38 "
And Here Is The Other Feedback
setting BinaryFormatter.AssemblyFormat to Simple has no effect.
http://lab.msdn.microsoft.com/productfeedback/ViewFeedback.aspx?FeedbackID=c2045aff-b884-4052-bbfe-3c8c87361396
And Finally Is This Feedback
BadImageFormatException: Not to possible to perform a call with reflection on a generic method on a .NET remoting transparent proxy
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=059c20b0-f9b5-4982-a822-9c41fcc5efdf
Above-mention in Chinese are not sure accurate error message in Visual Studio 2005 CHT version, there are traslated by myself.
So, You can look English version for accurate error message.
請注意最後三個Post。
特別是"Resolved as Postponed by Microsoft on 2005-09-26 at 13:02:38 "這一個。
下面是另一個類似的Feedback
BinaryFormatter.AssemblyFormat似乎沒有功能
http://lab.msdn.microsoft.com/productfeedback/ViewFeedback.aspx?FeedbackID=c2045aff-b884-4052-bbfe-3c8c87361396
最後是這個
錯誤訊息=BadImageFormatException:Not to possible to perform a call with reflection on a generic method on a .NET remoting transparent proxy
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=059c20b0-f9b5-4982-a822-9c41fcc5efdf
以上我沒去找相對映的中文錯誤訊息,有翻的都是按自己的意思翻,看英文比較準
----------------------------------------------------------------
Here are writting practice verion, welcome for comment.
BinaryFormatter fails to deserialize
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=b08b64df-49f2-44a3-ac10-7811281eb149
Please Focus on Last Tree Comments.
Especially "Resolved as Postponed by Microsoft on 2005-09-26 at 13:02:38 "
And Here Is The Other Feedback
setting BinaryFormatter.AssemblyFormat to Simple has no effect.
http://lab.msdn.microsoft.com/productfeedback/ViewFeedback.aspx?FeedbackID=c2045aff-b884-4052-bbfe-3c8c87361396
And Finally Is This Feedback
BadImageFormatException: Not to possible to perform a call with reflection on a generic method on a .NET remoting transparent proxy
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=059c20b0-f9b5-4982-a822-9c41fcc5efdf
Above-mention in Chinese are not sure accurate error message in Visual Studio 2005 CHT version, there are traslated by myself.
So, You can look English version for accurate error message.
Subscribe to:
Posts (Atom)