N
Neal
I have developed a simple membershipuser class and membershipprovider class.
I basically followed the procedures referenced at
http://msdn.microsoft.com/en-us/library/44w5aswa.aspx and made it even
simpler by inheriting sqlmembershipprovider, and did overrides on only a
couple of methods. So far, they seem to work fine when used in code.
However, I tried using the web site administration tool to edit a user and
received the error below. I am not planning to use WSAT in production; I
just want to make sure something is not wrong with my application. I figured
WSAT would still work (albeit without the additional property) since the
GetUser method returns a CustomMembershipUser which derives from
MembershipUser and should automatically be re-cast to MembershipUser (this
works fine in code when I programmatically get or update users). Any ideas?
Is this expected behavior between WSAT and a customized membershipuser class,
or have I done something wrong?
Could not load type 'CustomMembershipUser' from assembly 'App_Code.n0hgw1qq,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. at
System.Web.Administration.WebAdminPage.CallWebAdminHelperMethod(Boolean
isMembership, String methodName, Object[] parameters, Type[] paramTypes) at
ASP.security_users_edituser_aspx.Page_Load() in
c:\Windows\Microsoft.NET\Framework\v2.0.50727\ASP.NETWebAdminFiles\Security\Users\editUser.aspx:line
38 at System.Web.Util.CalliHelper.ArglessFunctionCaller(IntPtr fp, Object o)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at
System.Web.UI.Control.LoadRecursive() at
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,
Boolean includeStagesAfterAsyncPoint)
MembershipUser Class:
---------------------------
Imports Microsoft.VisualBasic
<Serializable()> _
Public Class CustomMembershipUser
Inherits MembershipUser
Private _CustId As String
Public Property CustId() As String
Get
Return _CustId
End Get
Set(ByVal value As String)
_CustId = value
End Set
End Property
Public Sub New(ByVal providername As String, _
ByVal username As String, _
ByVal userid As Object, _
ByVal email As String, _
ByVal passwordQuestion As String, _
ByVal comment As String, _
ByVal isApproved As Boolean, _
ByVal isLockedOut As Boolean, _
ByVal creationDate As DateTime, _
ByVal lastLoginDate As DateTime, _
ByVal lastActivityDate As DateTime, _
ByVal lastPasswordChangedDate As DateTime, _
ByVal lastLockOutDate As DateTime, _
ByVal custId As String)
MyBase.New(providername, _
username, _
userid, _
email, _
passwordQuestion, _
comment, _
isApproved, _
isLockedOut, _
creationDate, _
lastLoginDate, _
lastActivityDate, _
lastPasswordChangedDate, _
lastLockOutDate)
Me.CustId = custId
End Sub
End Class
MembershipProvider class:
--------------------------------
Imports Microsoft.VisualBasic
Imports System.Web.Security
Imports System.Data
Imports System.Data.SqlClient
Public Class CustomMembershipProvider
Inherits SqlMembershipProvider
Private pApplicationName As String = "/"
Private conn As SqlConnection = New SqlConnection("Data
Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;
Integrated Security=True; User Instance=True")
'CustomMembershipProvider.UpdateUser
Public Overrides Sub UpdateUser(ByVal user As MembershipUser)
Dim cmd As SqlCommand = New SqlCommand("UPDATE
vw_aspnet_MembershipUsers " & _
" SET Email = @Email, Comment = @Comment," & _
" IsApproved = @IsApproved, CustId = @CustId" & _
" WHERE Username = @Username", conn)
Dim u As CustomMembershipUser = CType(user, CustomMembershipUser)
If u.CustId = Nothing Then u.CustId = ""
If u.Comment = Nothing Then u.Comment = ""
If pApplicationName = Nothing Then pApplicationName = "/"
cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 128).Value = u.Email
cmd.Parameters.Add("@Comment", SqlDbType.NText, 3000).Value =
u.Comment
cmd.Parameters.Add("@IsApproved", SqlDbType.Bit).Value = u.IsApproved
cmd.Parameters.Add("@CustId", SqlDbType.NVarChar, 255).Value =
u.CustId
cmd.Parameters.Add("@Username", SqlDbType.NVarChar, 255).Value =
u.UserName
'cmd.Parameters.Add("@ApplicationName", SqlDbType.NVarChar,
255).Value = pApplicationName
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch e As SqlException
Throw e
Finally
conn.Close()
End Try
End Sub
' MembershipProvider.GetUser(String, Boolean)
Public Overrides Function GetUser(ByVal username As String, _
ByVal userIsOnline As Boolean) As
MembershipUser
Dim cmd As SqlCommand = New SqlCommand("SELECT Userid, Username,
Email, PasswordQuestion," & _
" Comment, IsApproved, IsLockedOut, createdate,
LastLoginDate," & _
" LastActivityDate, LastPasswordChangedDate, LastLockOutDate,"
& _
" CustId" & _
" FROM vw_aspnet_MembershipUsers WHERE Username = @Username",
conn)
cmd.Parameters.Add("@Username", SqlDbType.VarChar, 255).Value =
username
'cmd.Parameters.Add("@ApplicationName", SqlDbType.VarChar,
255).Value = pApplicationName
Dim u As CustomMembershipUser = Nothing
Dim reader As SqlDataReader = Nothing
Try
conn.Open()
reader = cmd.ExecuteReader()
If reader.HasRows Then
reader.Read()
u = GetUserFromReader(reader)
If userIsOnline Then
Dim updateCmd As SqlCommand = New SqlCommand("UPDATE
vw_aspnet_MembershipUsers " & _
"SET LastActivityDate = @LastActivityDate " & _
"WHERE Username = @Username", conn)
updateCmd.Parameters.Add("@LastActivityDate",
SqlDbType.DateTime).Value = DateTime.Now
updateCmd.Parameters.Add("@Username", SqlDbType.VarChar,
255).Value = username
'updateCmd.Parameters.Add("@ApplicationName",
SqlDbType.VarChar, 255).Value = pApplicationName
updateCmd.ExecuteNonQuery()
End If
End If
Catch e As SqlException
Throw e
Finally
If Not reader Is Nothing Then reader.Close()
conn.Close()
End Try
Return u
End Function
' GetUserFromReader
' A helper function that takes the current row from the SqlDataReader
' and hydrates a MembershiUser from the values. Called by the
' MembershipUser.GetUser implementation.
Private Function GetUserFromReader(ByVal reader As SqlDataReader) As
CustomMembershipUser
Dim Userid As Object = reader.GetValue(0)
Dim username As String = reader.GetString(1)
Dim email As String = reader.GetString(2)
Dim passwordQuestion As String = ""
If Not reader.GetValue(3) Is DBNull.Value Then _
passwordQuestion = reader.GetString(3)
Dim comment As String = ""
If Not reader.GetValue(4) Is DBNull.Value Then _
comment = reader.GetString(4)
Dim isApproved As Boolean = reader.GetBoolean(5)
Dim isLockedOut As Boolean = reader.GetBoolean(6)
Dim creationdate As DateTime = reader.GetDateTime(7)
Dim lastLoginDate As DateTime = New DateTime()
If Not reader.GetValue(8) Is DBNull.Value Then _
lastLoginDate = reader.GetDateTime(8)
Dim lastActivityDate As DateTime = reader.GetDateTime(9)
Dim lastPasswordChangedDate As DateTime = reader.GetDateTime(10)
Dim lastlockoutdate As DateTime = New DateTime()
If Not reader.GetValue(11) Is DBNull.Value Then _
lastlockoutdate = reader.GetDateTime(11)
Dim custId As String = String.Empty
If reader.GetValue(12) IsNot DBNull.Value Then _
custId = reader.GetString(12)
Dim u As CustomMembershipUser = New CustomMembershipUser(Me.Name, _
username, _
Userid, _
email, _
passwordQuestion, _
comment, _
isApproved, _
isLockedOut, _
creationdate, _
lastLoginDate, _
lastActivityDate, _
lastPasswordChangedDate, _
lastlockoutdate, _
custId)
Return u
End Function
End Class
web.config membership section:
-------------------------------------
<membership defaultProvider="CustomMembershipProvider">
<providers>
<clear />
<add connectionStringName="LocalSqlServer"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
applicationName="/" requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
name="CustomMembershipProvider"
type="CustomMembershipProvider" />
</providers>
</membership>
I basically followed the procedures referenced at
http://msdn.microsoft.com/en-us/library/44w5aswa.aspx and made it even
simpler by inheriting sqlmembershipprovider, and did overrides on only a
couple of methods. So far, they seem to work fine when used in code.
However, I tried using the web site administration tool to edit a user and
received the error below. I am not planning to use WSAT in production; I
just want to make sure something is not wrong with my application. I figured
WSAT would still work (albeit without the additional property) since the
GetUser method returns a CustomMembershipUser which derives from
MembershipUser and should automatically be re-cast to MembershipUser (this
works fine in code when I programmatically get or update users). Any ideas?
Is this expected behavior between WSAT and a customized membershipuser class,
or have I done something wrong?
Could not load type 'CustomMembershipUser' from assembly 'App_Code.n0hgw1qq,
Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. at
System.Web.Administration.WebAdminPage.CallWebAdminHelperMethod(Boolean
isMembership, String methodName, Object[] parameters, Type[] paramTypes) at
ASP.security_users_edituser_aspx.Page_Load() in
c:\Windows\Microsoft.NET\Framework\v2.0.50727\ASP.NETWebAdminFiles\Security\Users\editUser.aspx:line
38 at System.Web.Util.CalliHelper.ArglessFunctionCaller(IntPtr fp, Object o)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at
System.Web.UI.Control.LoadRecursive() at
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,
Boolean includeStagesAfterAsyncPoint)
MembershipUser Class:
---------------------------
Imports Microsoft.VisualBasic
<Serializable()> _
Public Class CustomMembershipUser
Inherits MembershipUser
Private _CustId As String
Public Property CustId() As String
Get
Return _CustId
End Get
Set(ByVal value As String)
_CustId = value
End Set
End Property
Public Sub New(ByVal providername As String, _
ByVal username As String, _
ByVal userid As Object, _
ByVal email As String, _
ByVal passwordQuestion As String, _
ByVal comment As String, _
ByVal isApproved As Boolean, _
ByVal isLockedOut As Boolean, _
ByVal creationDate As DateTime, _
ByVal lastLoginDate As DateTime, _
ByVal lastActivityDate As DateTime, _
ByVal lastPasswordChangedDate As DateTime, _
ByVal lastLockOutDate As DateTime, _
ByVal custId As String)
MyBase.New(providername, _
username, _
userid, _
email, _
passwordQuestion, _
comment, _
isApproved, _
isLockedOut, _
creationDate, _
lastLoginDate, _
lastActivityDate, _
lastPasswordChangedDate, _
lastLockOutDate)
Me.CustId = custId
End Sub
End Class
MembershipProvider class:
--------------------------------
Imports Microsoft.VisualBasic
Imports System.Web.Security
Imports System.Data
Imports System.Data.SqlClient
Public Class CustomMembershipProvider
Inherits SqlMembershipProvider
Private pApplicationName As String = "/"
Private conn As SqlConnection = New SqlConnection("Data
Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;
Integrated Security=True; User Instance=True")
'CustomMembershipProvider.UpdateUser
Public Overrides Sub UpdateUser(ByVal user As MembershipUser)
Dim cmd As SqlCommand = New SqlCommand("UPDATE
vw_aspnet_MembershipUsers " & _
" SET Email = @Email, Comment = @Comment," & _
" IsApproved = @IsApproved, CustId = @CustId" & _
" WHERE Username = @Username", conn)
Dim u As CustomMembershipUser = CType(user, CustomMembershipUser)
If u.CustId = Nothing Then u.CustId = ""
If u.Comment = Nothing Then u.Comment = ""
If pApplicationName = Nothing Then pApplicationName = "/"
cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 128).Value = u.Email
cmd.Parameters.Add("@Comment", SqlDbType.NText, 3000).Value =
u.Comment
cmd.Parameters.Add("@IsApproved", SqlDbType.Bit).Value = u.IsApproved
cmd.Parameters.Add("@CustId", SqlDbType.NVarChar, 255).Value =
u.CustId
cmd.Parameters.Add("@Username", SqlDbType.NVarChar, 255).Value =
u.UserName
'cmd.Parameters.Add("@ApplicationName", SqlDbType.NVarChar,
255).Value = pApplicationName
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch e As SqlException
Throw e
Finally
conn.Close()
End Try
End Sub
' MembershipProvider.GetUser(String, Boolean)
Public Overrides Function GetUser(ByVal username As String, _
ByVal userIsOnline As Boolean) As
MembershipUser
Dim cmd As SqlCommand = New SqlCommand("SELECT Userid, Username,
Email, PasswordQuestion," & _
" Comment, IsApproved, IsLockedOut, createdate,
LastLoginDate," & _
" LastActivityDate, LastPasswordChangedDate, LastLockOutDate,"
& _
" CustId" & _
" FROM vw_aspnet_MembershipUsers WHERE Username = @Username",
conn)
cmd.Parameters.Add("@Username", SqlDbType.VarChar, 255).Value =
username
'cmd.Parameters.Add("@ApplicationName", SqlDbType.VarChar,
255).Value = pApplicationName
Dim u As CustomMembershipUser = Nothing
Dim reader As SqlDataReader = Nothing
Try
conn.Open()
reader = cmd.ExecuteReader()
If reader.HasRows Then
reader.Read()
u = GetUserFromReader(reader)
If userIsOnline Then
Dim updateCmd As SqlCommand = New SqlCommand("UPDATE
vw_aspnet_MembershipUsers " & _
"SET LastActivityDate = @LastActivityDate " & _
"WHERE Username = @Username", conn)
updateCmd.Parameters.Add("@LastActivityDate",
SqlDbType.DateTime).Value = DateTime.Now
updateCmd.Parameters.Add("@Username", SqlDbType.VarChar,
255).Value = username
'updateCmd.Parameters.Add("@ApplicationName",
SqlDbType.VarChar, 255).Value = pApplicationName
updateCmd.ExecuteNonQuery()
End If
End If
Catch e As SqlException
Throw e
Finally
If Not reader Is Nothing Then reader.Close()
conn.Close()
End Try
Return u
End Function
' GetUserFromReader
' A helper function that takes the current row from the SqlDataReader
' and hydrates a MembershiUser from the values. Called by the
' MembershipUser.GetUser implementation.
Private Function GetUserFromReader(ByVal reader As SqlDataReader) As
CustomMembershipUser
Dim Userid As Object = reader.GetValue(0)
Dim username As String = reader.GetString(1)
Dim email As String = reader.GetString(2)
Dim passwordQuestion As String = ""
If Not reader.GetValue(3) Is DBNull.Value Then _
passwordQuestion = reader.GetString(3)
Dim comment As String = ""
If Not reader.GetValue(4) Is DBNull.Value Then _
comment = reader.GetString(4)
Dim isApproved As Boolean = reader.GetBoolean(5)
Dim isLockedOut As Boolean = reader.GetBoolean(6)
Dim creationdate As DateTime = reader.GetDateTime(7)
Dim lastLoginDate As DateTime = New DateTime()
If Not reader.GetValue(8) Is DBNull.Value Then _
lastLoginDate = reader.GetDateTime(8)
Dim lastActivityDate As DateTime = reader.GetDateTime(9)
Dim lastPasswordChangedDate As DateTime = reader.GetDateTime(10)
Dim lastlockoutdate As DateTime = New DateTime()
If Not reader.GetValue(11) Is DBNull.Value Then _
lastlockoutdate = reader.GetDateTime(11)
Dim custId As String = String.Empty
If reader.GetValue(12) IsNot DBNull.Value Then _
custId = reader.GetString(12)
Dim u As CustomMembershipUser = New CustomMembershipUser(Me.Name, _
username, _
Userid, _
email, _
passwordQuestion, _
comment, _
isApproved, _
isLockedOut, _
creationdate, _
lastLoginDate, _
lastActivityDate, _
lastPasswordChangedDate, _
lastlockoutdate, _
custId)
Return u
End Function
End Class
web.config membership section:
-------------------------------------
<membership defaultProvider="CustomMembershipProvider">
<providers>
<clear />
<add connectionStringName="LocalSqlServer"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
applicationName="/" requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
name="CustomMembershipProvider"
type="CustomMembershipProvider" />
</providers>
</membership>