Bryuk
Bryuk

Reputation: 3345

How to switch Authentication in MVC Application?

I've created Internet MVC Application with Individual User Accounts Authentication, but now this project should be intranet with windows authentication... How to switch authentication, when project is almost done? I'm not guru in MVC and this is new technology for me, so any help please and if possible with all steps in description=)

Upvotes: 15

Views: 40871

Answers (4)

Adam R. Grey
Adam R. Grey

Reputation: 2024

Since I found this question through google attempting the same thing, and Firearm's link doesn't quite do the process justice, I'll attempt to list the steps I went through here. Obviously, if I tell you to remove something, that only means if you aren't using it otherwise. I don't think you have to do these steps in any particular order. Also, I'm using Entity Framework, so you'll have to look elsewhere to remove it.

  1. in the solution explorer, highlight your project and press f4. This will bring up the properties window for that project. Disable anonymous authentication. Enable windows authentication.
  2. Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution... uninstall anything with "owin" in the name, Microsoft.AspNet.Identity.EntityFramework, and Microsoft.AspNet.Identity.Core.
  3. Open your Web.config. Under runtime, under assemblyBinding, remove all the dependentAssembly's for Owin stuff that got left behind. Under system.web, replace <authentication mode="None" /> with <authentication mode="Windows" /> <authorization> <deny users="?" /> </authorization>. Under system.webServer, remove handlers. under modules, remove <remove name="FormsAuthentication" />.
  4. Remove the Account and Manage controllers and views. Remove the ManageViewModels from your models.
  5. Under App_Start, get rid of IdentityConfig and Startup.Auth.
  6. At the top level, right next to your web config, is Startup.cs. Get rid of it.
  7. Make a new ApplicationDbContext. It should derive from DbContext. Get rid of throwIfV1Schema: false in your constructors. Then you can get rid of IdentityModels from your Models folder. Add a new migration and update your database.
  8. Obviously you'll have to clean out any references you've made yourself to Identity.

Possible additional step: * remove _LoginPartial view. The _Layout view will then be updated to replace partial display of that view with this line:

 <p class="nav navbar-text navbar-right">Hello, @User.Identity.Name!</p>

Upvotes: 11

chriszo111
chriszo111

Reputation: 363

Searching the exact same problem led me to this article, however the answers are a bit old, so with ASP.NET using MVC 5 this should be detailed documentation from Microsoft:

  1. To detect Windows Authentication in an MVC project, the wizard looks for the authentication element from your web.config file.

    <configuration>
        <system.web>
            <authentication mode="Windows" />
        </system.web>
    </configuration>
    
  2. To detect Windows Authentication in a Web API project, the wizard looks for the IISExpressWindowsAuthentication element from your project's .csproj file:

    <Project>
        <PropertyGroup>
            <IISExpressWindowsAuthentication>enabled
            </IISExpressWindowsAuthentication>
        </PropertyGroup>
    </Project>
    

Found at Diagnosing errors with the Azure Active Directory Connection Wizard

For my specific problem it was switching to Azure AD rather than Windows Authentication (which was preset), there are more steps found at the developer network website.

Upvotes: 3

Dennis Betten
Dennis Betten

Reputation: 48

I'm afraid I'm a bit late with my answer to you're question on how to implement the SwitchUser functionality, but for those of you who are still struggling with this (even Microsoft SharePoint still can't get it to work...), here's how it's done: (I just finished writing the article)

Switch User Functionality using MVC4 and Windows Authentication

If you need more information on how to get Windows Authentication workong for an Intranet Website using AD and Windows Server 2012 (or Higher), then take a look at my following article:

Windows Authentication on Intranet Website using AD and Windows Server 2012 (or Higher)

Happy coding!

Upvotes: 2

Firearm
Firearm

Reputation: 164

In the Web.config of you project. The first step would be change:

<authentication mode="Forms">
</authentication>

to

<authentication mode="Windows">
</authentication>

Selecting your project and hitting F4 for the properties window allows you to change the authentication method.

However instead of me putting step by step in here just use this very easy to follow tutorial: Enabling Windows Authentication

Upvotes: 15

Related Questions