Tom
Tom

Reputation: 213

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)

Upvotes: 7

Views: 1007

Answers (1)

pijemcolu
pijemcolu

Reputation: 2605

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;

Upvotes: 1

Related Questions