1

I have Visual Studio 2013 Ultimate. I have created ASP.NET Web Forms Application (not MVC) with .Net Framework 4.
Users Primary Key is uniqueidentifier (GUID), I found in dafault Login.aspx, Register.aspx and other forms. There are codes using User.Identity.Name instead of Primary Key UserId :

 Dim hasLocalPassword = OpenAuth.HasLocalPassword(User.Identity.Name)
 Dim accounts As IEnumerable(Of OpenAuthAccountData) = OpenAuth.GetAccountsForUser(User.Identity.Name)
 Dim result As SetPasswordResult = OpenAuth.AddLocalPassword(User.Identity.Name, password.Text)

How to change this 'User.Identity.Name' to Primary Key GUID of users, because more than two users can have same name?

johna
  • 10,540
  • 14
  • 47
  • 72
Er Mayank
  • 1,017
  • 2
  • 16
  • 39

1 Answers1

2

If you are using AspNet membership providers :

MembershipUser user = Membership.GetUser(User.Identity.Name);
Guid guid = (Guid)user.ProviderUserKey;

In case of Oauth you have do do this.

You should add `[InitializeSimpleMembership]` on top of controller class if you use another controller than AccountController. 

WebSecurity.GetUserId(User.Identity.Name);

Also check this answer: MVC Simplemembership I cannot get the userID after logging a user in via OAuth

Community
  • 1
  • 1
Ajay Kelkar
  • 4,591
  • 4
  • 30
  • 29
  • 1
    I have two users of same name. how to do that. This AspNet Membership does not allow another user of same name?? – Er Mayank Jul 27 '14 at 12:41
  • Is there any way to use UserId as Primary key, and want to create UserS with SAME NAME?? – Er Mayank Jul 27 '14 at 12:53
  • I Dont know why Microsoft have created this (User.Identity.Name) for Membership defaults because there can be more users with same name.. –  Jul 27 '14 at 12:55
  • Anytime it will have value based on current logged in user,don't allow duplicate user names in you app. First name and last name can be same but username has to be unique. – Ajay Kelkar Jul 27 '14 at 13:32