Pick up only the user name in AD

5

With this code, I bring everything, Domain / User.

ViewBag.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

I would like to bring only the username. I can get all that, and get everything that comes after the "/" bar and work with it, but I find that sort of Gambi. I would like to know if there is a way to bring only the name, otherwise I will go the way up (gambi).

    
asked by anonymous 24.09.2014 / 16:26

1 answer

2

The code below (I ran in a console application) does the split you need and returns the user:

using System.Security.Principal;

string user = WindowsIdentity.GetCurrent().Name.Split('\')[1].Trim();
Console.WriteLine("Usuário: " + user);
Console.Read();

Following full console application code:

using System;
using System.Security.Principal;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string user = WindowsIdentity.GetCurrent().Name.Split('\')[1].Trim();
            Console.WriteLine("Usuário: " + user);
            Console.Read();
        }
    }
}
    
24.09.2014 / 16:55