Login with Windows Authentication in IIS

4

I have a SqlServer 2008 base where all users can connect to it via Windows domain, this works correctly via SQL Management Studio.

I'm doing a C # Application in MVC, and wanted it when the user accessed the site, I could get that login and password from windows and move on to mount my Connection String .

My Connection String looks like this:

conexao = new SqlConnection("Data Source=vc9;Initial Catalog=sgt;Integrated Security=SSPI;Application Name='Controle de Sprint';Pooling=False;User ID=dominio\usuario;Password=senha;");

So it works, but I can not get C # to catch the Windows user of the user who is accessing the site.

How can this be done?

    
asked by anonymous 03.05.2016 / 14:17

2 answers

7

You can not get the password of the logged in user, this goes against Microsoft's security policy. You can configure an AD account in Application Pool of IIS to make the connection, and can get your system to get the user logged in (Windows) in your application, without problems. However, the password you can not.

For your system to get Windows authentication, just change your web.config to this:

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

For more information, see this answer .

And to get the user logged in, just use something like this:

var usuario = User.Identity.Name;

or depending on where you want this data, this way:

  var usuario = HttpContext.Current.User.Identity.Name

And if you do not forget to modify IIS to accept the connection via windows.

On this site you will find a tutorial on how to do this.

    
03.05.2016 / 14:44
3

You must first configure your IIS to perform authentication using Windows Authentication.

To do this in the development environment, open visual studio, right mouse click on the project, choose Use IIS Express , if it appears Use Visual Studio Developement Server is because it is already using IIS Express , then you do not have to take any further action.

Right-click again, go to Properties/Propriedades and set Anonymous Authentication to false and Windows Authentication to true .

Once the application has been published, you need to do the same in IIS where the application is hosted. to do this select your aplicação , then in the IIS choose Authentication tab. Again set Anonymous Authentication to false and Windows Authentication to true .

Once this is done, modify your connection to use Trusted_Connection or Integrated Security . it is not necessary to inform id and password , it will log on using the network user (windows).

<connectionStrings>
  <add name="MyDbConn1" connectionString="Server=MyServer;Database=MyDb;Trusted_Connection=Yes;"/>
  <add name="MyDbConn2" connectionString="Initial Catalog=MyDb;Data Source=MyServer;Integrated Security=SSPI;"/>
</connectionStrings> 
    
03.05.2016 / 14:44