Only get user name in AD

Asked

Viewed 1,794 times

5

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

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

I would like to bring only the user name. I can take all this, and take everything that comes after the "/" bar and work with it, but I think it’s kind of Gambi. I wonder if there is how to bring only the name straight, otherwise I will go the way up(Ambi).

  • I don’t see how you can go rogue. You just want some of the information.

1 answer

2


The code below (rode in a console application) does the split that 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();

Follow 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();
        }
    }
}
  • On the fly, Tiago.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.