Jaime Oliveira
Jaime Oliveira

Reputation: 761

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)

Upvotes: 16

Views: 24178

Answers (3)

Yeyeye
Yeyeye

Reputation: 29

someone in the C# discord community gave me a quick solution to this .

  1. Open command prompt on your local machine
  2. create a folder : "C:\Users\Prson\source\repos\AppName"
  3. Navigate to the folder : cd "C:\Users\Prson\source\repos\AppName"
  4. Type : dotnet new mvc --auth Individual --framework netcoreapp{type version}

5.resource

Upvotes: 0

Jaime Oliveira
Jaime Oliveira

Reputation: 761

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

Upvotes: 34

bsod_
bsod_

Reputation: 941

You would need to install Identity using nuget into your project. Here is a good StackOverflow thread that you can follow -

Adding ASP.NET MVC5 Identity Authentication to an existing project

Upvotes: 2

Related Questions