Login with network user (AD)

7

I need to log in to the user on my system, with the user logged in to the company network. I've never done this and am having second thoughts on how to get user in AD. I am doing research on the net and still can not understand how to search for this user. The question is: How do I get the user logged on to the network and soon on my system? I use MVC5, Visual Studio 2013, C #.

So, I got this code in another forum and it worked, but there is one boring thing still to be solved. It brings me the domain and / or machine name, in this format: Domain \ User. How do I remove the domain and the forward slash? Below is my solution:

public ActionResult CadastroCargo()
        {
            ViewBag.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

            return View();
        }
    
asked by anonymous 26.08.2014 / 13:36

2 answers

6

If you are going to create a new project, just use the ASP.NET Web Application project template, choose MVC, next to the option Change Authentication:

ThenchoosetheWindowsAuthenticationoption,whichisforintranetapplications.

Clickok,andyou'redone.YouhaveaWebApplicationpickinguptheADuser.

Noticethatintheapplication'sWeb.Configtheauthenticationpartwillbeasautheticationmode="Windows":

 <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Windows" />
    <authorization>
      <deny users="?" />
    </authorization>
  </system.web>

If you want to limit which users or groups will access your site, simply add the rules, for example:

 <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Windows" />
    <authorization>
      <allow roles="meudominio\Grupo1,meudominio\Grupo2" />
      <allow users="meudominio\usuario1" />
      <deny users="*" />
    </authorization>
  </system.web>

This way you block access for all users, but allow all users of Group1 and Group2, and user1 to access.

Simple like this.

    
26.08.2014 / 14:50
2

Hello,

If your intention is just to write on the screen the user name without the domain in front of the user name, you can use the following command

System.Environment.UserName
    
24.03.2015 / 19:13