Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User.Identity.Name returns guid

For some (obscure) reason my MVC 4 application returns a guid when I run:

var name = User.Identity.Name;

I have also tested this in a brand new MVC 4 application and this is new behavior to me. When I look up the documentation on IIdentity it states (as I remembered) that Identity.Name should

Gets the name of the current user.

Why is it returning a guid in my case?

From the web.config

<authentication mode="Forms">
    <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>

More relevant information: The application is also deployed on another machine, talking to the same database. We use the value of "User.Identity.Name" in that database and when it's not there for a user (new user) we add a new user with that guid.

Now, what is strange: when you switch applications (from localhost to the one deployed on T) you need to log in again. The User.Identity.Name will then be set to a new guid.

(with the starter default application we don't talk to the DB of course, but the same thing happens; User.Identity.Name returns a guid)

like image 853
Tom Avatar asked Aug 31 '16 09:08

Tom


People also ask

What is user identity name?

User-id definitionThe user name, or username, by which a person is identified to a computer system or network. A user commonly must enter both a user ID and a password as an authentication mechanism during the logon process.

What does HttpContext current user identity name return?

It just holds the username of the user that is currently logged in. After login successful authentication, the username is automatically stored by login authentication system to "HttpContext.Current.User.Identity.Name" property.

Where is HttpContext user stored?

It is most likely stored in Managed Passwords: Click Start > Run. Enter "control userpasswords2"


Video Answer


1 Answers

You'll need to find the generic principal object which corresponds to the current user.

using System.Security.Principal;
...

GenericPrincipal genericPrincipal = GetGenericPrincipal();
GenericIdentity principalIdentity = (GenericIdentity)genericPrincipal.Identity;
string fullName = principalIdentity.Name;
like image 93
pijemcolu Avatar answered Oct 05 '22 11:10

pijemcolu