如何从数据库中提取 GUID 以更新用户名
我有一个与 ASP.net 中的 SQL 数据库不同的 SQL 数据库,因此我必须创建一个自定义成员资格提供程序。获取用户的提供者如下所示: 我的 SQL 类名称是 CustomSqlProvider:
Public Overrides Function GetUser(ByVal username As String, _
ByVal userIsOnline As Boolean) As MembershipUser
'Dim connectionString As String = "Server=***;Database=***;User Id=***password=****"'
Dim conn As SqlConnection = New SqlConnection(connectionString)
Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@UserName", SqlDbType.NVarChar)
cmd.Parameters.Add("@Password", SqlDbType.NVarChar)
Dim u As MembershipUser = Nothing
Dim reader As SqlDataReader = Nothing
Try
conn.Open()
If userIsOnline Then
Dim updateCmd As SqlCommand = New SqlCommand("Update_User", conn)
updateCmd.Parameters.Add("@UserName", SqlDbType.NVarChar)
updateCmd.Parameters.Add("@F_Name", SqlDbType.NVarChar)
updateCmd.Parameters.Add("@L_Name", SqlDbType.NVarChar)
updateCmd.Parameters.Add("@PWD", SqlDbType.VarChar)
updateCmd.Parameters.Add("@Email", SqlDbType.VarChar)
updateCmd.ExecuteNonQuery()
End If
Catch e As SqlException
'If WriteExceptionsToEventLog Then
' WriteToEventLog(e, "Get_User, as String")
' Throw New ProviderException(exceptionMessage)
'Else
'Throw e
'End If
Finally
If Not reader Is Nothing Then reader.Close()
conn.Close()
End Try
Return u
End Function
Public Overrides Function GetUser(ByVal providerUserKey As Object, _
ByVal userIsOnline As Boolean) As MembershipUser
Dim conn As SqlConnection = New SqlConnection(connectionString)
Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = providerUserKey
Dim u As MembershipUser = 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_User", conn)
updateCmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier)
updateCmd.ExecuteNonQuery()
End If
End If
Catch e As SqlException
If WriteExceptionsToEventLog Then
WriteToEventLog(e, "GetUser(Object, Boolean)")
Throw New ProviderException(exceptionMessage)
Else
Throw e
End If
Finally
If Not reader Is Nothing Then reader.Close()
conn.Close()
End Try
Return u
End Function
现在我想做的是首先登录我的网站,它进行身份验证,然后我就进入了。然后我被重定向到另一个页面,我可以在其中更改我的用户名。但我似乎无法获取登录用户的 GUID 来更改它。我正在使用更新用户表的存储过程。
我在页面上有以下代码,我可以在其中更改凭据:
Dim currentUser as MembershipUser = Membership.GetUser() Dim CurrentUSerId as Guid = CType(currentUser.ProviderUserKey, Guid)
我得到的引用未设置为对象实例。任何帮助将不胜感激。
感谢您的编辑。我还添加了获取和设置属性,以便我能够向这些参数添加值。 Update_User 存储过程中的参数是输入。我的 Get_User 存储过程看起来像这样..这就是为什么我认为它没有获取 GUID,因为当它运行时,它会要求用户名和密码,然后它返回包括 Guid 在内的所有内容:
GO
/****** Object: StoredProcedure [dbo].[Get_User] Script Date: 07/14/2010 09:16:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Get_User]
@UserName nvarchar(50),
@Password varchar(50)
AS
SELECT USER_ID, USER_NAME, F_NAME, L_NAME, BUILDING_ID, SIP_ROLE, INTERNAL_ID, PWD, EMAIL FROM dbo.USERS WHERE USER_NAME = @UserName AND PWD = @Password
RETURN
/////////// /////////////////////////////////////////////////////////// ///////////////////////////////
添加这些获取和设置并将参数变量分配给我的 Sproc 参数后,出现以下错误:
未设置对对象实例的引用。
在这行代码中:
Dim currentUser As MembershipUser = Membership.GetUser()
Dim UserId As Guid = CType(currentUser.ProviderUserKey, Guid)
这就是我在 UserInfo.aspx.vb 页面上单击按钮下的内容:
Dim sql As New SqlClient.SqlCommand("Update_User", con)
sql.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = UserId
sql.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = txtUserName.Text
I have a SQL database that is different than the one in ASP.net so I had to create a custom membership Provider. The provider to get the user looks like this:
My SQL class name is CustomSqlProvider:
Public Overrides Function GetUser(ByVal username As String, _
ByVal userIsOnline As Boolean) As MembershipUser
'Dim connectionString As String = "Server=***;Database=***;User Id=***password=****"'
Dim conn As SqlConnection = New SqlConnection(connectionString)
Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@UserName", SqlDbType.NVarChar)
cmd.Parameters.Add("@Password", SqlDbType.NVarChar)
Dim u As MembershipUser = Nothing
Dim reader As SqlDataReader = Nothing
Try
conn.Open()
If userIsOnline Then
Dim updateCmd As SqlCommand = New SqlCommand("Update_User", conn)
updateCmd.Parameters.Add("@UserName", SqlDbType.NVarChar)
updateCmd.Parameters.Add("@F_Name", SqlDbType.NVarChar)
updateCmd.Parameters.Add("@L_Name", SqlDbType.NVarChar)
updateCmd.Parameters.Add("@PWD", SqlDbType.VarChar)
updateCmd.Parameters.Add("@Email", SqlDbType.VarChar)
updateCmd.ExecuteNonQuery()
End If
Catch e As SqlException
'If WriteExceptionsToEventLog Then
' WriteToEventLog(e, "Get_User, as String")
' Throw New ProviderException(exceptionMessage)
'Else
'Throw e
'End If
Finally
If Not reader Is Nothing Then reader.Close()
conn.Close()
End Try
Return u
End Function
Public Overrides Function GetUser(ByVal providerUserKey As Object, _
ByVal userIsOnline As Boolean) As MembershipUser
Dim conn As SqlConnection = New SqlConnection(connectionString)
Dim cmd As SqlCommand = New SqlCommand("Get_User", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = providerUserKey
Dim u As MembershipUser = 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_User", conn)
updateCmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier)
updateCmd.ExecuteNonQuery()
End If
End If
Catch e As SqlException
If WriteExceptionsToEventLog Then
WriteToEventLog(e, "GetUser(Object, Boolean)")
Throw New ProviderException(exceptionMessage)
Else
Throw e
End If
Finally
If Not reader Is Nothing Then reader.Close()
conn.Close()
End Try
Return u
End Function
Now what I want to do is I first login to my website, it authenticates, and I am in. I am then redirected to another page where I can change my Username. But I can't seem to get the GUID of the logged in User in order to change it. I'm using a stored procedure that Updates the Users Table.
I have the following code on the Page where I can change credentials:
Dim currentUser as MembershipUser = Membership.GetUser()
Dim CurrentUSerId as Guid = CType(currentUser.ProviderUserKey, Guid)
I get reference not set to an instance of an object. Any help would be appreciated.
Thank you for the edit. I have also added get and set properties so I can be able to add values to those parameters. The parameters in the Update_User sproc are inputs. My Get_User sproc looks like this..which is why I think it is not getting the GUID because when it is run it asks for Username and password and then it returns everything including the Guid back:
GO
/****** Object: StoredProcedure [dbo].[Get_User] Script Date: 07/14/2010 09:16:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Get_User]
@UserName nvarchar(50),
@Password varchar(50)
AS
SELECT USER_ID, USER_NAME, F_NAME, L_NAME, BUILDING_ID, SIP_ROLE, INTERNAL_ID, PWD, EMAIL FROM dbo.USERS WHERE USER_NAME = @UserName AND PWD = @Password
RETURN
///////////////////////////////////////////////////////////////////////////////////////////
After adding those gets and sets and assigning parameter variables to my Sproc parameters I get the following error:
Reference not set to an instance of an object.
at this line of code:
Dim currentUser As MembershipUser = Membership.GetUser()
Dim UserId As Guid = CType(currentUser.ProviderUserKey, Guid)
And this is what I have on the UserInfo.aspx.vb Page under the Button click:
Dim sql As New SqlClient.SqlCommand("Update_User", con)
sql.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = UserId
sql.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = txtUserName.Text
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一个 GetUser OverRide 中,它在哪里执行以下行:
我看不到该命令已执行,但众所周知,我非常愚蠢:)
也许您的代码需要遵循 Get_User by ProviderUserKey 模式中的模式,我添加了一些缺失的内容和问题,希望这会有所帮助。
In the first GetUser OverRide where does it execute the following line:
I can't see that the command is executed, I have been known to be incredibly dumb though :)
Perhaps your code needs to follow the pattern in the Get_User by ProviderUserKey pattern, I've added some missing things and questions, hope this helps.