<b> </b><b>使用</b><b>Profile.GetProfile(string username)</b><b>時碰上的問題</b><b></b>
<b></b>
我想在一個頁面裡編輯任意一個MemberShip的User的Profile,這時我使用了頁面.Profile.GetProfile(username),由于能登陸到這個頁面的使用者都是具有權限的使用者,是以運作的時候一切正常,為了優化代碼,我要把操作Profile的代碼移動到背景自己定義的類裡,這時候已經不存在頁面類的執行個體,是以我要使用HttpContext.Current.Profile來代替Page.Profile 這時候問題出現了,在HttpContext.Current.Profile裡不存在GetProfile(string username)這個方法,仔細看一下 原來這兩個Profile是來之不同的類,Page.Profile是ProfileComm的執行個體,而HttpContext.Current.Profile是Profilebase的執行個體,最後找到得到GetProfile的方法是這樣的:
ProfileCommon pc = (ProfileCommon)ProfileBase.Create(“username”, true);
而這裡的username,一定是要一個通過認證的membership的user,這時候在使用pc.GetProfile(“otherusername”) 就可以對其他使用者的Profile進行編輯了。
<b>2. </b><b>使用</b><b>MembershipUser.ChangePassword</b><b>碰上的問題</b><b></b>
我自定義了一個添加,修改,删除Membership使用者的控件,要使用到修改密碼的功能,MembershipUser裡有一個ChangePassword的方法,不過需要兩個參數,oldpassword和newpassword,由于我需要一個修改密碼而不用提供舊密碼的功能,是以就要先把使用者的老密碼給找出來,幸運的發現在MembershipUser下有一個GetPassword的方法,立即調用彈出異常,看了異常的說明繼續找問題,發現問題出在 Membership Provider的配置參數上,
<membership defaultProvider="herSqlMembershipProvider">
<providers>
<add name="herSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider"requiresQuestionAndAnswer="false" connectionStringName="her" requiresUniqueEmail="false"passwordFormat="Clear" minRequiredPasswordLength="3" enablePasswordRetrieval="true"
minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"passwordStrengthRegularExpression="" applicationName="/ " />
</providers>
</membership>
GetPassword主要涉及到三個屬性: passwordFormat, requiresQuestionAndAnswer, enablePasswordRetrieval,首先需要把enablePasswordRetrieval設定為true,可以回收密碼,然後是passwordFormat可以設定三個值:Clear, Encrypted, and Hashed,如果設定成了Hashed,那密碼是不可逆的,也不能得到密碼,而Clear和Encrypted都是可以的,Clear是明文儲存,而Encrypted是加密儲存,如果設定成了加密儲存,還需要設定一些密鑰什麼的,不然CreateUser時會出錯,最後是屬性requiresQuestionAndAnswer,它是表示是否需要有密碼問題和答案來重新取回密碼的機制,如果這個設定為true,那麼你在使用GetPassword時,需要把這個使用者的問題的答案一起傳進去,不然也會産生異常。
<b>3. </b><b>匿名通路時設定</b><b>Profile</b><b>屬性出現異常</b><b></b>
這也是需要配置檔案裡的屬性進行配置:
<anonymousIdentification enabled="true" />
<profile defaultProvider="herSqlProfile">
<add name="herSqlProfile" type="System.Web.Profile.SqlProfileProvider"connectionStringName="her" />
<properties>
<add name="MyTheme" type="String" allowAnonymous="true"/>
</properties>
</profile>
首先是anonymousIdentification 把enbaled設定為true,然後再profile裡的屬性allowAnonymous="true"
路漫漫其修遠兮 吾将上下而求索
本文轉自 lu xu 部落格園部落格,原文連結: http://www.cnblogs.com/dotLive/archive/2007/03/15/676112.html ,如需轉載請自行聯系原作者