p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Professional (http://p2p.wrox.com/forumdisplay.php?f=215)
-   -   Return UserId from aspnet_Users (http://p2p.wrox.com/showthread.php?t=48651)

rao965 October 4th, 2006 05:32 PM

Return UserId from aspnet_Users
 
Hi;

I am tring to grab UserId from aspnet_users table in aspnet.db after user X loggen in and put it in a nother table as forien key in my custom databse.

could anyone let me know how i can do that

many thanks


timeware October 5th, 2006 03:41 AM

Once the user has logged in then you can find out User.Identity.Name

You can then use this to return the UserId and therefore reference to aspnet_Membership, aspnet_sersInRoles etc.

If anyone knows a more elegant way then please post it here as I would also be interested


hcusto October 5th, 2006 04:04 AM

First include "System.Web.Security" namespace

Code:

using System.Web.Security;
then in your method (function) where you want to get user id write:

 Membership.GetUser().ProviderUserKey;

if you want to add it to the string you must convert it to string but first check if the user id logged in
Code:

string userID;

if (User.Identity.IsAuthenticated)
{
      userID = Membership.GetUser().ProviderUserKey.ToString();
}

Hope that this helps,
Haris


surekha March 15th, 2007 12:17 AM

Hi,
    I am also trying to get userId. I am using the following command:
(Guid)Membership.GetUser().ProviderUserKey
I am assigning this value to a guid variable. But, I am getting a error like:

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="App_Web_ghqgbxbt"

Hope someone will help me.

Thanks

Tremmorkeep July 12th, 2007 04:50 PM

checking to see if the user is authenticated is critical at this point, my guess is that you dont have a user auth. so you're trying to get a providerkey for a null user. GetUser() also accepts a string argument so you can specify which user you want (ie GetUser("Steve") if you are editing data for users on your site.


Hope that helps.
-Matt


All times are GMT -4. The time now is 08:24 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.