5

I am building an intranet site in MVC3 @Razor. It is utilizing Windows Authentication. The problem I have is that all of the users in the database are listed by UserID. The default action in getting the user information from the Windows context includes the domain (e.g. domain\userid). Is it possible to change this to just get the userid and ignore the domain, without having to write a custom authentication process? Meaning User.Identity.Name is just the userid and not the domain and userid.

EDIT: This is based off of a membership model using the default membership.

EDIT: The problem has nothing to do with the display of the information. The issue is when the system gets the users information from Windows and then goes to get the membership information, can the domain be removed, so only the userid is passed instead of the domain and userid. All I want is to be able to pass the userid to the membership instead of the "domain\userid".

1
  • For the visually inclined, it might be nice if you included a code snippet. Sep 15, 2012 at 14:52

2 Answers 2

14

The following:

User.Identity.Name.Split('\\')[1]; 

will remove the domain part.

4
  • That only works on a display. That does nothing to solve my issue. My problem is what is being passed to the membership during the authentication process. Sep 14, 2012 at 21:15
  • @user1672410 - You haven't fully described your issue. You're only describing what you think the answer is. What is your ACTUAL problem, not what you think will solve your problem. Sep 14, 2012 at 21:17
  • 2
    your line should be User.Identity.Name.Split('\\')[1]; as \ is an escape character. May 1, 2013 at 11:43
  • Razor - no semi-colon so --> @User.Identity.Name.Split('\\')[1] Sep 15, 2014 at 19:01
0

I used it like so on the View.

MVC View:

<a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">
  <span class="glyphicon glyphicon-user" style="color:white"></span> 
    Hello @UserManager.GetUserName(User).Split('@')[0]!
</a>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.