使用MemberShip,Profile时碰上的一些问题
1.????? 使用Profile.GetProfile(string username)時(shí)碰上的問(wèn)題<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
我想在一個(gè)頁(yè)面里編輯任意一個(gè)MemberShip的User的Profile,這時(shí)我使用了頁(yè)面.Profile.GetProfile(username),由于能登陸到這個(gè)頁(yè)面的用戶都是具有權(quán)限的用戶,所以運(yùn)行的時(shí)候一切正常,為了優(yōu)化代碼,我要把操作Profile的代碼移動(dòng)到后臺(tái)自己定義的類里,這時(shí)候已經(jīng)不存在頁(yè)面類的實(shí)例,所以我要使用HttpContext.Current.Profile來(lái)代替Page.Profile 這時(shí)候問(wèn)題出現(xiàn)了,在HttpContext.Current.Profile里不存在GetProfile(string username)這個(gè)方法,仔細(xì)看一下 原來(lái)這兩個(gè)Profile是來(lái)之不同的類,Page.Profile是ProfileComm的實(shí)例,而HttpContext.Current.Profile是Profilebase的實(shí)例,最后找到得到GetProfile的方法是這樣的:
ProfileCommon pc = (ProfileCommon)ProfileBase.Create(“username”, true);
而這里的username,一定是要一個(gè)通過(guò)認(rèn)證的membership的user,這時(shí)候在使用pc.GetProfile(“otherusername”) 就可以對(duì)其他用戶的Profile進(jìn)行編輯了。
2.????? 使用MembershipUser.ChangePassword碰上的問(wèn)題
我自定義了一個(gè)添加,修改,刪除Membership用戶的控件,要使用到修改密碼的功能,MembershipUser里有一個(gè)ChangePassword的方法,不過(guò)需要兩個(gè)參數(shù),oldpassword和newpassword,由于我需要一個(gè)修改密碼而不用提供舊密碼的功能,所以就要先把用戶的老密碼給找出來(lái),幸運(yùn)的發(fā)現(xiàn)在MembershipUser下有一個(gè)GetPassword的方法,立即調(diào)用彈出異常,看了異常的說(shuō)明繼續(xù)找問(wèn)題,發(fā)現(xiàn)問(wèn)題出在 Membership Provider的配置參數(shù)上,
?
??? <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主要涉及到三個(gè)屬性: passwordFormat, requiresQuestionAndAnswer, enablePasswordRetrieval,首先需要把enablePasswordRetrieval設(shè)置為true,可以回收密碼,然后是passwordFormat可以設(shè)置三個(gè)值:Clear, Encrypted, and Hashed,如果設(shè)置成了Hashed,那密碼是不可逆的,也不能得到密碼,而Clear和Encrypted都是可以的,Clear是明文保存,而Encrypted是加密保存,如果設(shè)置成了加密保存,還需要設(shè)置一些密鑰什么的,不然CreateUser時(shí)會(huì)出錯(cuò),最后是屬性requiresQuestionAndAnswer,它是表示是否需要有密碼問(wèn)題和答案來(lái)重新取回密碼的機(jī)制,如果這個(gè)設(shè)置為true,那么你在使用GetPassword時(shí),需要把這個(gè)用戶的問(wèn)題的答案一起傳進(jìn)去,不然也會(huì)產(chǎn)生異常。
3.????? 匿名訪問(wèn)時(shí)設(shè)置Profile屬性出現(xiàn)異常
這也是需要配置文件里的屬性進(jìn)行配置:
??? <anonymousIdentification enabled="true" />
??? <profile defaultProvider="herSqlProfile">
????? <providers>
??????? <add name="herSqlProfile" type="System.Web.Profile.SqlProfileProvider" connectionStringName="her" />
????? </providers>
????? <properties>
??????? <add name="MyTheme" type="String" allowAnonymous="true"/>
????? </properties>
</profile>
首先是anonymousIdentification 把enbaled設(shè)置為true,然后再profile里的屬性allowAnonymous="true"
轉(zhuǎn)載于:https://www.cnblogs.com/dotLive/archive/2007/03/15/676112.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的使用MemberShip,Profile时碰上的一些问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Intellij Idea使用教程汇总篇
- 下一篇: iOS开发技巧 - Size Class