46

I have tried my best to search the web before asking this question. I've seen similar questions on stackoverflow, however, none has been answered satisfactorily for a long time now. This is one more attempt to get this recurring question answered.

The Problem

How to build an ASP.NET MVC 5 website which uses "Windows Auth" for Intranet users and "Forms Auth" for Internet users? We'd like to accomplish this using ASP.NET Identity. Moreover, we don't want to use Active Directory Groups for authorization. For Intranet users, we want to authenticate them using Active Directory and then fall back to ASP.NET Identity to manage their roles and other profile data.

It'll be nice if we don't ask the end user to choose auth method. The web app should log in intranet users seamlessly. They shouldn't even know that there is a login screen. Likewise, the internet users shouldn't be asked to enter their domain credentials. They should see form based login screen right away.

Is there any recommended way of solving this? Or could you comment if any of the following are proper solutions?

http://world.episerver.com/blogs/Dan-Matthews/Dates/2014/8/Mixing-Forms-and-Windows-Authentication/

https://github.com/MohammadYounes/MVC5-MixedAuth

http://mvolo.com/iis-70-twolevel-authentication-with-forms-authentication-and-windows-authentication/

FYI This is 2004 article, may not be helpful now: https://msdn.microsoft.com/en-us/library/ms972958.aspx

Ravi M Patel
  • 2,905
  • 2
  • 23
  • 32
  • did you managed to achieve this in the end? I'm looking for similar solution however with added complexity of providing solution along with Active Directory for Azure too – siddhant Kumar Aug 12 '16 at 09:22
  • Ravi, did you able to arrive the solution if yes please share the solution with us. – Aji Nov 30 '16 at 11:41
  • Not yet, will post when I do. – Ravi M Patel Nov 30 '16 at 15:54
  • Did you find solution? – Akmal Salikhov Apr 05 '18 at 07:03
  • 1
    No, I quit trying. We managed with a single login page to manage both account types. If user is from domain (we can figure that out from @example.com hostname) we use LDAP otherwise we use forms authentication. So windows domain users don't have to create new account and remember one more password. – Ravi M Patel Apr 13 '18 at 17:52

6 Answers6

7

IIS configuration
Enable Anonymous Authentication status in IIS for the whole site and Windows Authentication for some folder under root directory (for example, /WindowsLogin). In this folder place aspx file (for WebForms project) or create ApiController (for MVC project).

Site setup
On login page add button “Login with Windows/ActiveDirectory account” (in similar way as it is common practice to add buttons Login with Twitter, Facebook, Gmail, etc.). When user presses this button, they will be redirected to the page or controller in /WindowsLogin folder, which require Windows authentication. If site uses some Single Sign-On functionality, locate it in that page or controller, in other case just save Session for Windows users there. If user accessed that page or controller, they had been authenticated as Windows users already.

rba
  • 338
  • 2
  • 11
4

One of the possible ways could be creating two sites in IIS, but having the same target folder, where sources of site are located. First site is for internal users with enabled Windows Authentication mode and binding to 80 port, while second site is for external users with Anonymous mode enabled and binding to 8080 port, for example. Then, on firewall you will have to configure NAT, that all requests coming from within local network or VPN, will be redirected to local IIS server on port 80 and all requests coming from Internet, will be redirected to port 8080 of IIS server.

rba
  • 338
  • 2
  • 11
  • I guess it will help. However, it will also cause additional effort of maintaining another site. It'll also change site url. Any idea, how you'd connect windows auth with role defined with asp.net identity and stored in a database? – Ravi M Patel Feb 08 '15 at 21:11
  • No additional efforts to maintain site. You just create two links in IIS pointing to the same site on disk. And URL will be one: all local network is behind firewall and only firewall has an external IP address, to which domain is bound. So domain (URL) remains the same for all users, all logic is hidden behind firewall. – rba Feb 09 '15 at 06:22
  • 1
    But then you can't login as an external, non-AD user from the LAN? – Bigwave Feb 18 '15 at 10:36
4

The term for this is Mixed-Mode Authentication. I have done this multiple times. You only need to tweak your main site. Here is how I have done it.

Keep your main MVC site as-is but run it as Anonymous vs. under Windows Auth.

Internal Site

Create a Redirect URL Site: Setup this site as Window Auth so you can pull the User ID from Active Directory. Give your users this URL and/or make it the link they click on your Intranet. Then this site calls your MVC Site and passes the user credentials (login id).

a. This can be done either via an encrypted string on the URL or encrypted value in a cookie. You can encrypt with an expiration date/time value too.

b. (Speaking from Forms Auth) Create a Forms Authentication Ticket with that user ID. Run any other login logic you have. Done.

External Site - No Changes required. Let the users login as-is.

Elim Garak
  • 1,728
  • 1
  • 16
  • 21
  • But then you have two URLs? Then less technically savy users who are only sometimes in the office will have to remember two urls and when to use them? – Bigwave Feb 18 '15 at 10:10
  • Yes two URLs, but if you look at even the Microsoft docs on how to hack-up a site to make it work with Windows Auth and Anonymous they show how to do it, but advise against it for security reasons. Do you have an Intranet? Put the link there. – Elim Garak Feb 19 '15 at 00:28
  • 1
    @Bigwave there are multiple ways you could accomplish this suggestion without multiple URLs. You could configure your DNS to give an internal IP to intranet users, so e.g. www.example.com points to 10.x.x.x on intranet but your public IP externally. Then set up the small Windows Auth app as a sub-site under the same URL, and for internal users redirect anonymous users there. Or if you're using a load balancer, add a specific "X-Forwarded-For" header for external users and look for that. Maybe not the cleanest solution but with a bit of work you could make it as seamless as possible. – Tobias J Sep 09 '15 at 15:44
2

Are you wanting to handle forms and AD authentication from one URL? I have used thinktecture (claims based auth) as the framework for WIF and marshaling various forms of authentication. However to handle if from one URL I had to handle some logic at login that associated the user to AD or Forms based. In a more recent project, this was handled at user management when we created the user account (it was associated to AD of Forms Auth). Then when the user logged in they would preface the AD domain name as part of the login. There are a number of ways to implement this, this was just one I have used. An example, instead of requiring the domain, just use the username, then check for AD or forms based flags on the username and then handle authentication accordingly

EDIT Just an update in re-reading your question. Are the internet users and intranet users the same? If so you need to just go forms based auth across the board and manage the users in the product DB independent of AD. If they are the same then they could login prefacing the domain name to username. if you wanted to rely solely on AD.

rlcrews
  • 3,482
  • 19
  • 66
  • 116
  • 1
    No, intranet and internet users are DIFFERENT. We want to user windows auth for intranet users because they all use company laptops \ PCs to access the website. We already know who they are as they have logged on to their computers using their windows account. Then it wouldn't make sense to ask them to login once again. Hence windows auth. Internet users are external vendors etc, so they have to provide their user name and password to log on. – Ravi M Patel Feb 09 '15 at 05:48
2

I did a proof of concept of this some time ago, at my previous job, so the details are hazy and I don't have any code to refer to...

The requirements were:

  • Single URL for internal (LAN) and external (internet) access
  • Two types of users, people on the domain and external (non-AD) users
  • Windows authentication for domain users both internally and externally
  • The ability to enter domain logon details when using iPads (no windows auth)

The core idea in the solution I came up with was that we used Active Directory Group Policy to add a custom string to http request header user agent, the content doesn't matter, in fact we used a long random string of characters.

https://technet.microsoft.com/en-us/library/cc770379.aspx

Then the landing page for the site checks for this, and if found redirects to a virtual directory, with windows auth, that checked their AD account, populated the ASP.NET authentication token and then redirected them to their home page.

If the custom header isn't there then it just displayed the normal login form.

The only other thing was to add an AD email/password check to the normal login form so that if a domain user accessed the site from a non-windows device (iPad) then they could use their normal login details.

Bigwave
  • 2,166
  • 1
  • 17
  • 28
0

Why not put your website code on the server, robocopy it to two separate websites and just handle the changes in authentication by configuring the web.config. (one would be setup with anonymous and one with windows authentication.)

It's not as snazzy as other methods but it's relatively painless. There are two sites but the content (except for the web.config) are identical.

Lareau
  • 1,982
  • 1
  • 26
  • 47