7

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)

Geoff James
  • 3,122
  • 1
  • 17
  • 36
Tom
  • 213
  • 1
  • 8
  • 1
    What do you use for *aothentication/authorization*? – Adil Mammadov Aug 31 '16 at 10:12
  • It's just the default template for MVC4 internet applications: which is the silly thing; you typically don't have to think about these things... – Tom Aug 31 '16 at 10:45
  • Perhaps [this](http://stackoverflow.com/questions/924692/how-do-you-get-the-userid-of-a-user-object-in-asp-net-mvc) may be useful for you -OR- [link](https://coderwall.com/p/qmsqya/how-to-get-current-user-id-in-asp-net-mvc-4) may also be useful – Syed Fasih Aug 31 '16 at 10:50
  • Checked the links: if I'm not mistaken those are for finding out **what** guid belongs to a user. I have the exact opposite question: why is there a guid at all?! – Tom Aug 31 '16 at 11:06
  • you didn't change anything else with the model/context/...? – Matthias Burger Aug 31 '16 at 11:10
  • I have created default MVC4 application and UserName is email in my project. – Adil Mammadov Aug 31 '16 at 11:17
  • @MatthiasBurger: no I haven't. – Tom Aug 31 '16 at 11:42
  • @Adil Mammadov: that is what I would expect, but for some reason it's not working like that on my system. Now what could it be? – Tom Aug 31 '16 at 11:42
  • @Tom Could you create [mcve](http://stackoverflow.com/help/mcve), and upload it to GitHub? – Win Mar 21 '17 at 21:06
  • @Tom - what kind of authentication do you use? Email&password or OAuth? – mariozski Mar 22 '17 at 23:09

1 Answers1

1

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;
pijemcolu
  • 2,257
  • 22
  • 36