我创建了一个与CMS的API集成的自定义配置文件提供程序。在为经过身份验证的用户(Profile.FirstName
)提取数据时,它可以正常工作,但在创建新用户的配置文件时会出错。
以下是web.config
中的部分<profile enabled="true" defaultProvider="CustomProfileProvider" inherits="objProfile">
<providers>
<clear />
<add name="CustomProfileProvider" type="CustomProfileProvider" />
</providers>
</profile>
这是objProfile类
Public Class objProfile
Inherits ProfileBase
Public Property FirstName() As String
Get
Return Me.GetPropertyValue("FirstName")
End Get
Set(ByVal value As String)
Me.SetPropertyValue("FirstName", value)
End Set
End Property
Public Property LastName() As String
Get
Return Me.GetPropertyValue("LastName")
End Get
Set(ByVal value As String)
Me.SetPropertyValue("LastName", value)
End Set
End Property
Public Property Email() As String
Get
Return Me.GetPropertyValue("Email")
End Get
Set(ByVal value As String)
Me.SetPropertyValue("Email", value)
End Set
End Property
Public Property Address1() As String
Get
Return Me.GetPropertyValue("Address1")
End Get
Set(ByVal value As String)
Me.SetPropertyValue("Address1", value)
End Set
End Property
...
Public Property MailList() As Boolean
Get
Return Me.GetPropertyValue("Mailing List")
End Get
Set(ByVal value As Boolean)
Me.SetPropertyValue("Mailing List", value)
End Set
End Property
End Class
这是自定义ProfileProvider。现在唯一实现的函数是GetPropertyValues
,因为这是在单步执行调试器时使用的唯一函数。我将根据需要实施其他人。
Public Class CustomProfileProvider
Inherits ProfileProvider
Public Overrides Property ApplicationName() As String
Get
Return ConfigurationManager.AppSettings("ApplicationName")
End Get
Set(ByVal value As String)
Return
End Set
End Property
Public Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection)
MyBase.Initialize(name, config)
End Sub
Public Overrides Function DeleteInactiveProfiles(ByVal authenticationOption As System.Web.Profile.ProfileAuthenticationOption, ByVal userInactiveSinceDate As Date) As Integer
End Function
Public Overloads Overrides Function DeleteProfiles(ByVal usernames() As String) As Integer
End Function
Public Overloads Overrides Function DeleteProfiles(ByVal profiles As System.Web.Profile.ProfileInfoCollection) As Integer
End Function
Public Overrides Function FindInactiveProfilesByUserName(ByVal authenticationOption As System.Web.Profile.ProfileAuthenticationOption, ByVal usernameToMatch As String, ByVal userInactiveSinceDate As Date, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Profile.ProfileInfoCollection
End Function
Public Overrides Function FindProfilesByUserName(ByVal authenticationOption As System.Web.Profile.ProfileAuthenticationOption, ByVal usernameToMatch As String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Profile.ProfileInfoCollection
End Function
Public Overrides Function GetAllInactiveProfiles(ByVal authenticationOption As System.Web.Profile.ProfileAuthenticationOption, ByVal userInactiveSinceDate As Date, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Profile.ProfileInfoCollection
End Function
Public Overrides Function GetAllProfiles(ByVal authenticationOption As System.Web.Profile.ProfileAuthenticationOption, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Web.Profile.ProfileInfoCollection
End Function
Public Overrides Function GetNumberOfInactiveProfiles(ByVal authenticationOption As System.Web.Profile.ProfileAuthenticationOption, ByVal userInactiveSinceDate As Date) As Integer
End Function
Public Overrides Function GetPropertyValues(ByVal cotext As System.Configuration.SettingsContext, ByVal collection As System.Configuration.SettingsPropertyCollection) As System.Configuration.SettingsPropertyValueCollection
Dim PropertyValueCollection As New System.Configuration.SettingsPropertyValueCollection
... (get user properties from cms and put into PropertyValueCollection) ...
'return the PropertyValueCollection
Return PropertyValueCollection
End Function
Public Overrides Sub SetPropertyValues(ByVal context As System.Configuration.SettingsContext, ByVal collection As System.Configuration.SettingsPropertyValueCollection)
End Sub
End Class
在具有登录用户的页面上,Profile.FirstName
正常工作。但是,当我创建新用户,然后使用objProfile.Create(UserName)
创建配置文件时,所有属性都会出现The settings property 'Mailing List' was not found.
答案 0 :(得分:0)
我发现了问题。我上周都病了,所以我责备了。
在GetPropertyValues
函数中获取个人资料时,我使用的是HttpContext.Current
而不是传入的context
参数。
Public Overrides Function GetPropertyValues(ByVal context As System.Configuration.SettingsContext, ByVal collection As System.Configuration.SettingsPropertyCollection) As System.Configuration.SettingsPropertyValueCollection
Dim PropertyValueCollection As New System.Configuration.SettingsPropertyValueCollection
'get username from parameter, context.Item("UserName"), not HttpContext.Current.User.Identity.Name.ToString()
...get user properties from CMS and put into PropertyValueCollection...
'return the PropertyValueCollection
Return PropertyValueCollection
End Function