Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC: Change from "No Authentication" to "Individual User Accounts" on existing project

I am following a course on ASP.Net MVC and reached a point where the course relies on the standard "Register" and "Log In" functions that Visual Studio automatically builds when you select "Individual User Accounts" when creating the proj.

Apparently, newer versions of Visual Studio now default to "No Authentication".

I've built a lot of stuff and it would be a pain to create a new project and move everything over. Is there an easier way of changing the authentication type (and generating the respective pages and controllers)?

(I don't want to add identity and tweak the authentication process manually, I want to have the same result as if I was creating a new project with "Individual User Accounts" set - with VS creating default controllers and views)

like image 457
Jaime Oliveira Avatar asked Aug 09 '18 18:08

Jaime Oliveira


People also ask

How do I change no authentication to individual user accounts?

From the ASP.NET project window select "WebAPI". Click on the "Change Authentication" button. From the Change Authentication dialog select "Individual User Accounts". Click on the "OK" button.

How do I change the authentication method in ASP.NET MVC?

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

How do I change the authentication in Visual Studio project?

Select File >> New >> select ASP.NET Core Web Application, and change the authentication to Windows Authentication. We can also configure the existing application for Windows Authentication by selecting the option of WA. To configure the authentication manually, open Visual Studio project properties >> go to Debug tab.

How do I add Owin to an existing project?

Add OWIN startup and authentication configuration classesIn Solution Explorer, right-click your project, select Add, and then Add New Item. In the search text box dialog, type "owin". Name the class "Startup" and select Add.


1 Answers

Inserted a point you missed after 6th step. Also thanks for the solution !

Found a solution!

  1. Create a new dummy project of the same type, but with the "Individual User Accounts" authentication type selected. This will generate the files you need.

  2. On the current project (with "No Authentication"), use the Package Manager Console to add the following references (the ones you don't have yet):

  • Install-Package EntityFramework
  • Install-Package EntityFramework.SqlServerCompact
  • Install-Package Microsoft.AspNet.Identity.Core
  • Install-Package Microsoft.AspNet.Identity.EntityFramework
  • Install-Package Microsoft.AspNet.Identity.Owin
  • Install-Package Microsoft.AspNet.WebApi.Client
  • Install-Package Microsoft.Owin.Host.SystemWeb
  1. Copy the following files from dummy project to live project (and add them to the solution, obviously):
  • \App_Start\IdentityConfig.cs
  • \App_Start\Startup.Auth.cs
  • \Controllers\AccountController.cs
  • \Controllers\ManageController.cs
  • \Models\AccountViewModels.cs
  • \Models\IdentityModels.cs
  • \Models\ManageViewModels.cs
  • \Views\Accounts\ (entire content)
  • \Views\Manage\ (entire content)
  • \Views\Shared\ _LoginPartial.cshtml
  • \Startup.cs
  1. After adding these files to the solution, change their namespaces according to live project (replace all).

  2. Open "IdentityModels.cs" and change the ConnectionString in "ApplicationDbContext" to match your live one (as in Web.config)

  3. Open your \Views\Shared_Layout.cshtml file and add

    @Html.Partial(“_LoginPartial”)

7) Copy the ... from Web.config of Dummy project to the main project.(You missed this point which might result in issues like database not being created)

  1. Compile and test. You should be all set!

All credits to https://chiroldes.wordpress.com/2015/04/02/agregando-asp-net-identity-a-un-proyecto-mvc-sin-validacion-autenticacion-o-autorizacion-de-usuarios/comment-page-1/#comment-13

like image 115
Jaime Oliveira Avatar answered Oct 04 '22 03:10

Jaime Oliveira