Get UserID from ASP.Net Login control LoggedIn event

荒凉一梦 提交于 2019-11-30 21:53:26

ScottGu has the answer to this problem, as explained in this blog post on the ASP.NET Forum:

The LoggedIn event fires immediately after the user is logged in with the login control -- but not before the next request to the site. As such, the User object isn't filled out yet.

If you change your code to something like:

Dim UserId As String
UserID = Membership.GetUser(Login1.UserName).ProviderUserKey.ToString()

It should work fine. Calling Membership.GetUser without passing the Username as a parameter will expect to grab the "current" logged in user. Of course, this fails for the above reasons as the User object is not yet populated. By passing the Login control's UserName property as a parameter to the GetUser() method, you explicitly force the Membership.GetUser method to retrieve the user as specified by name from the user store (i.e. database). This ensures that the Membership.GetUser method returns a valid MembershipUser object which allows you to access the ProviderUserKey property.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!