Er Mayank
Er Mayank

Reputation: 1073

how to use UserId instead of user name in authentication ( Web Forms asp.net)?

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?

Upvotes: 1

Views: 701

Answers (1)

Ajay Kelkar
Ajay Kelkar

Reputation: 4621

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

Upvotes: 2

Related Questions