Configure ASP.NET MVC for authentication against AD

前端 未结 3 1357
悲&欢浪女
悲&欢浪女 2020-11-30 18:32

What are the high level steps to authenticate users of an ASP.NET MVC application against Active Directory?

I presume something like:

  1. Modify web.config
3条回答
  •  孤城傲影
    2020-11-30 18:46

    Forms Authentication

    You can use the normal forms authentication to authenticate a user against an Active Directory, for that you just need you AD connection string:

    
      
    
    

    and add the Membership Provider to use this connection:

    
      
        
      
    
    

    you will need to use username@domain to successfully authenticate the user.

    Here is something to get you started

    • http://helios.ca/2009/05/04/aspnet-mvc-forms-authentication-with-active-directory/

    Windows Authentication

    If you start your project new, you can always select Intranet application from the template and all is taken care for you

    enter image description here

    If you want to do it manually, you need to change:

    1. Enable Windows Authentication
    2. Disable Anonymous authentication

    for detailed info on doing this on IIS7/8 and IISExpress:

    IIS 7 & IIS 8

    1. Open IIS Manager and navigate to your website.
    2. In Features View, double-click Authentication.
    3. On the Authentication page, select Windows authentication. If Windows authentication is not an option, you'll need to make sure Windows authentication is installed on the server.

      To enable Windows authentication on Windows: a) In Control Panel open "Programs and Features". b) Select "Turn Windows features on or off". c) Navigate to Internet Information Services > World Wide Web Services > Security and make sure the Windows authentication node is checked.

      To enable Windows authentication on Windows Server: a) In Server Manager, select Web Server (IIS) and click Add Role Services b) Navigate to Web Server > Security and make sure the Windows authentication node is checked.

    4. In the Actions pane, click Enable to use Windows authentication.

    5. On the Authentication page, select Anonymous authentication.
    6. In the Actions pane, click Disable to disable anonymous authentication.

    IIS Express

    1. Right click on the project in Visual Studio and select Use IIS Express.
    2. Click on your project in the Solution Explorer to select the project.
    3. If the Properties pane is not open, open it (F4).
    4. In the Properties pane for your project: a) Set "Anonymous Authentication" to "Disabled". b) Set "Windows Authentication" to "Enabled".

    In your web.config have something like

    
      
    
      
        
      
    
    

    and that's it!

    Now, when you want the user identity, just call

    @User.Identity.Name
    

    and this will show you the Domain\Username like for me :

    enter image description here

    Here is something to get you started

    • http://www.asp.net/mvc/tutorials/older-versions/security/authenticating-users-with-windows-authentication-cs

提交回复
热议问题