How to switch Authentication in MVC Application? - asp.net-mvc

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=)

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

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.
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.
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.
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" />.
Remove the Account and Manage controllers and views. Remove the ManageViewModels from your models.
Under App_Start, get rid of IdentityConfig and Startup.Auth.
At the top level, right next to your web config, is Startup.cs. Get rid of it.
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.
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>

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:
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>
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.

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!

Related

Two MVC Projects, sharing Login, not supposed to

I have an MVC5 project (with other underlying projects), using Web Forms Authentication (SimpleMembership).
Later, I created a second MVC project in the same solution. I changed the port for IISExpress debugging to be different than that of the original so I can access both sites when I debug.
http://localhost:12345/MainAppIndex
http://localhost:54321/SecondaryAppIndex
My current login information however, is shared between both projects. If I log in the first site and open the second, it uses my first login's credentials.
I literally made the second app by copying the first project, tweaking around the .csproj file and solution file, then stripped a bunch of stuff away to start with what I needed. (I also created a new aspnet membership database and pointed the new project to that db, so both apps have their own membership database).
What setting do I have to change for IIS Express to view these as two different logins?
And... will this be an issue in production if a user happens to look at both at the same time (or even swapping back and forth with only one open at once?)
This turned out to be a cookie issue. By changing the web.config to set a non-default cookie name (the default being .ASPXAUTH, apparently), they are then treated as separate logins.
<system.web>
<authentication mode="Forms">
<forms name=".ASPXAUTH-PROVIDERS" loginUrl="~/Account/Login" timeout="2880" />
</authentication>
</system.web>
You need to use another database or 2 applications will continue using the same database.
In order to do that, go to Web.Config (at the root) and use other Connection String.
Then go to Models -> IdentityModels.cs and change the name of the Connection string to your new connection string.

mvc3 windows authentication returning 401.0 Unauthorized

I am building a local intranet web application which uses the current user's AD credentials to access the system. My web server is running IIS 7.5 on Server 2008.
My AD is configured with two groups, Users and Admins, both of which I want to access my web application and I have added the two groups (read/execute permissions) to my directory in IIS. My web.config is pretty standard and I do not have any additional filters in my Global.asax file.
I am able to access my application if I use my user or admin account, but my coworker can not, which further complicates things. My accounts do not have any permissions on the folder - only the groups to which they belong.
Authentication mode is set to Windows, but unfortunately every attempt as my coworker to access the website fails with the following:
HTTP Error 401.0 - Unauthorized
You do not have permission to view this directory or page.
The "most likely cause" box suggests:
The authenticated user does not have access to a resource needed to process the request.
I just can not figure this out. If I create a sample mvc3 application and deploy it in the same directory, I can authenticate and view pages fine as any user.
There can be many reasons for this.
Your controller needs to be decorated with appropriate authorization attribute like following
[Authorize(Roles = "Administrator")]
In your web.config, you need to have following code inside your system.web configuration
<roleManager enabled="true" defaultProvider="YourRoleProvider">
Finally you need to see if that user has been added to their respective roles
I'd the exact same issue. After having few hours of research, it seems to be very easy way to deal with this issue.
<system.webServer>
<modules>
<!--<remove name="FormsAuthentication" />-->
</modules>
</system.webServer>
I'd to remove <remove name="FormsAuthentication" /> statement from modules attribute in Web.config file which was added by default while creating default MVC website.
I had the same problem in MVC 4, and I have resolved it with this:
[AllowAnonymous]
(Add this line in controller, above action you are calling when this error appears.)

How do I allow reflection on a shared IIS host?

In this question a user replied that he was able to override the security settings on a shared host. I'm using the same host, but haven't been able to figure out how to change web.config to allow reflection. Is it a one-liner?
I'm using MVC + Nhibernate in my project, but I can't even get an Mvc "new project" template site to work, I suppose the problem is reflection.
Try adding the following line to your web.config:
<system.web>
<trust Level="Full" />
</system.web>
Note, however, that the section might be locked by machine.config.

User.Identity.Name blank in ASP.Net MVC

Exactly as per the title.
Simply in my HomeController I have:
string Username = User.Identity.Name;
Why is this value always blank?
Is there something special I have to place in the web.config to get this value to come through. I've tried on both the VS dev web server as well as on a windows server 2003 IIS server.
It's got to be something simple, a server setting in IIS or something as the code is so simple and seems to be the correct way to reference this value.
Thx a lot
If you are wanting to use windows authentication it's not enough to just add
...
<system.web>
...
<authentication mode="Windows"/>
...
</system.web>
...
You also want to check the readme file instructions for the MVC project, especially if your running in IIS Express and using Visual Studio 2012.
See below:
Hosting on IIS Express:
Click on your project in the Solution Explorer to select the project.
If the Properties pane is not open, open it (F4).
In the Properties pane for your project:
a) Set "Anonymous Authentication" to "Disabled".
b) Set "Windows Authentication" to
"Enabled".
Hosting on IIS 7 or later:
Open IIS Manager and navigate to your website.
In Features View, double-click Authentication.
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.
In the Actions pane, click Enable to use Windows authentication.
On the Authentication page, select Anonymous authentication.
In the Actions pane, click Disable to disable anonymous authentication.
Sure is mate. You need to authenticate with the website. That's the name used for authentication.
You are authenticating, right?
It's not a setting, etc.
Click the LOG IN link, if you're using the stock standard ASP.NET MVC template (if my memory serves me right).
UPDATE (as the posted has added more info/comments)
So what you're after is Windows Authentication. A quick google search came up with this post. It's pretty helpful (though a bit old, but still relevant) .. check that out.
Found a better post with MVC code for Windows Authentication. Check that out instead.
Config setting that is important, is...
...
<system.web>
...
<authentication mode="Windows"/>
...
</system.web>
...
If you do not wish to Authorize on every controller or action, you can do a blanket authorization in the web.config file. This should work as long as you are using Windows authentication. If you allow ASP.NET to control the authentication, then you would not need to configure any IIS setting. It should then work well with whatever web server that you are running on. I do not know or assume what u have tried so far I'll try to be complete in my answer. First remark off the forms authentication tag in web.config. All following settings are placed in the system.web configuration section.
<!--
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
-->
Replace with the Windows authentication tag.
<authentication mode="Windows" />
Then add the authorization tag to deny access to anonymous users. If the users are using Internet Explorer and are connecting from an Intranet zone, IE automatically will login the user. But if the user is connecting from the Internet zone, IE will still display a login box though for safety. But these are options that can be set from IE.
<authorization>
<deny users="?" />
</authorization>
Setting authentication mode alone without authorization does not force the user to be authenticated in ASP.NET. If you want to control the security from IIS, I cannot help much with IIS settings but I know basically you can disable Basic Authentication , then enable Integrated Windows Authentication and then disable the Anonymous Login Account which will achieve the same or better results.
I am also working on an MVC project at the moment and I tested the above settings with my project and it works. You would not need the Authorize attribute since we have the authorization tag in the configuration. I hope this can be of help to you and not get another -1.
Have you attached Authorize attribute to ur action or controller?
public class HomeController : Controller {
[Authorize]
public ActionResult Index() {
string userName = User.Identity.Name;
return View();
}
}

Why is my ASP.NET MVC Application requesting Windows Authentication?

I've been building a ASP.NET MVC application and using forms authentication. In my controller action I have:
[Authorize(Users = "me,joe")]
that has been working great. Last night when I published the newest changes and attempt to view my website it started popping up a Windows Authentication dialog box. I've looked at all my code and cannot figure out WHY it would change to Windows authentication. My web.config file has not changed in at least 10 days. If I run the code from my dev box it does not do this...only when it is run from my host. And if I remove the Authorize line from my controller action it does not happen.
How can I fix this or how can I debug my solution to see why this is happening?
BTW, my web.config says:
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
This is most likely do to IIS settings for authentication and permissions on the website's folder on the web server. I would check both of those before anything else.
I'm just shooting in the dark, but do you have an <identity> setting in your web.config?
<system.web>
...
<identity impersonate="true"/>
...
</system.web>
If so, it might help to remove this line. It might also help to ask your hosting provider why Windows Authentication is suddenly being applied to your web site. As others have mentioned there are IIS settings which could be causing this behavior.
I think you need to set the permissions for IUSER, IWAM
Using the Windows Explorer browse to the folder you are wanting to grant permissions. Right click on the folder and select "properties". In the resulting dialog click on the "Security" tab near the top. You can then add or edit security for these accounts (IUSR_machineName, IWAM_macnineName, and ASPNET).

Resources