Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Windows Authentication in ASP.NET

I'm trying to use Windows Authentication in my ASP.NET application. Whenever I try to view the app it sends me to a login page. How can I make it work without having to manually login via the browser?

web.config

  <system.web>
    <authentication mode="Windows"></authentication>
    <anonymousIdentification enabled="false"/>
    <authorization>
      <deny users="?" />
      <allow users="*" />
    </authorization>
    <customErrors mode="Off"></customErrors>
    <identity impersonate="true"></identity>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime />
  </system.web>

error after updating IIS Express

Most likely causes:
No authentication protocol (including anonymous) is selected in IIS.
Only integrated authentication is enabled, and a client browser was used that does not support integrated authentication.
Integrated authentication is enabled and the request was sent through a proxy that changed the authentication headers before they reach the Web server.
The Web server is not configured for anonymous access and a required authorization header was not received.
The "configuration/system.webServer/authorization" configuration section may be explicitly denying the user access.

applicationhost.config

<authentication>
  <anonymousAuthentication enabled="false" />
  <basicAuthentication enabled="false" />
  <clientCertificateMappingAuthentication enabled="false" />
  <digestAuthentication enabled="false" />
  <iisClientCertificateMappingAuthentication enabled="false">
  </iisClientCertificateMappingAuthentication>

  <windowsAuthentication enabled="true">
    <providers>
      <add value="Negotiate" />
      <add value="NTLM" />
    </providers>
  </windowsAuthentication>
</authentication>
like image 737
Antarr Byrd Avatar asked Apr 29 '16 19:04

Antarr Byrd


People also ask

Does ASP.NET support Windows Authentication?

The ASP.NET Development Web Server also supports NTLM authentication. You can enable NTLM authentication by right-clicking the name of your project in the Solution Explorer window and selecting Properties.

How do I use Windows Authentication on a Web application?

You need to disable the "Anonymous Authentication" and Enable the "Windows Authentication". and right click your application -> Manage Application -> Browse. Here you need to give your windows user name and password. (the credentials you're given when you log in to your machine).

How does Windows Authentication work in C#?

Basic AuthenticationAfter a user provides built-in Windows user account information, the data is transmitted to the web server. Once IIS receives the authentication data, it attempts to authenticate the user with the corresponding Windows account. This password is encoded using Base64 and sent to the server.


Video Answer


1 Answers

Windows Authentication with IISExpress

Update your web.config

Make sure your web.config file both enables windows authentication and also denies anonymous authentication. HttpContext.Current.User.Identity.Name will be blank if the app falls through to anonymous authentication. Your config should look something like this:

<authentication mode="Windows" />
<authorization>
    <deny users="?"/>
</authorization>

Error 401.2 Unauthorized Sometimes, you might get the error 401.2 Unauthorized: Logon failed due to server configuration error. If you do, verify that you have permission to view this directory or page based on the credentials you supplied. Also make sure you have the authentication methods enabled on the Web server.

Updating applicationhost.config

You also might find you have to update the IISExpress applicationhost.config file (dont’ worry – I didn’t know it either). This is essentially the file version of the IIS configuration tool, where you can configure the web server itself. Finding the applicationhost.config file can be tricky. It might be in:

%userprofile%\documents\iisexpress\config\applicationhost.config

or

%userprofile%\my documents\iisexpress\config\applicationhost.config

Once you find it, update the following lines (paying special attention to enabled=true):

<windowsAuthentication enabled="true">
    <providers>
        <add value="Negotiate" />
        <add value="NTLM" />
    </providers>
</windowsAuthentication>

This is the article

like image 156
Muhammed Albarmavi Avatar answered Sep 19 '22 15:09

Muhammed Albarmavi