Reputation: 289
I need (I think) to get the current logged in userID so that I can update one of my tables that uses this userID as a foreign key. The problem is that the userID in the database does not match with this:
Guid currentUser = (Guid)Membership.GetUser().ProviderUserKey;
currentUser.toString();
results in dffaca0c-ae0b-8549-8073-1639985740be
whereas when I look in the database, it is 0CCAFADF0BAE498580731639985740BE
Why are they different values? (I only have one user). I am using an oracle database and provider for asp.net, but shouldn't make any difference.
Upvotes: 4
Views: 4424
Reputation: 31
I had the same issue and came up with this which resolved the issue:
public static string TranslateOraceEndianUserID()
{
MembershipUser myObject = Membership.GetUser();
Guid g = new Guid(myObject.ProviderUserKey.ToString());
byte[] b = g.ToByteArray();
string UserID = BitConverter.ToString(b, 0).Replace("-", string.Empty);
return UserID;
}
Upvotes: 3
Reputation: 57783
I believe these are the same values, but the display order is different. Looking at the 2 values:
dffaca0c-ae0b-8549-8073-1639985740be
0CCAFADF-0BAE-4985-8073-1639985740BE
The ordering of bytes for the first 3 segments is of a different order:
0CCA FADF => FADF 0CCA => DFFA CA0C == dffaca0c
0BAE => AE 0B == ae0b
4985 => 85 49 == 8549
As @x0n comments, this looks like a difference in endianness with Oracle. According the this description of the structure, the endianness of the first 8 bytes is system dependent, while the endianness of the last 8 bytes is specifically big endian.
Upvotes: 6
Reputation: 3716
You could always use the Lower Case User Name column to create the foreign Key. It is always unique. Maybe not the best option but it is the simplest and works well. I have used it in several projects.
Upvotes: 0
Reputation: 428
Or maybe also try using HttpContext.Curent.User to get the current user?
Upvotes: 0