16

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)

Jaime Oliveira
  • 751
  • 1
  • 5
  • 13

3 Answers3

34

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

Community
  • 1
  • 1
Jaime Oliveira
  • 751
  • 1
  • 5
  • 13
2

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

bsod_
  • 903
  • 8
  • 27
  • I've already added Identity to my project. I want to have the views and controllers that VS automatically generates when you create a new proj from scratch with the "Individual User Accounts" authentication type. – Jaime Oliveira Aug 09 '18 at 22:41
0

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

Yeyeye
  • 19
  • 10
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 02 '23 at 01:53