ASP.NET Web Forms and Identity: Move IdentityModel

2019-02-03 16:37发布

问题:

I'm trying to move IdentityModels.cs to another project to keep the web site apart from the Data Access Layer.

I followed this tutorial: http://blog.rebuildall.net/2013/10/22/Moving_ASP_NET_Identity_model_into_another_assembly

And also checked this question here: How to move MVC 5 IdentityModels.cs into a separate assembly

But I'm still confused because IdentityModels references another class called ApplicationUserManager as you can see bellow:

 public class ApplicationUser : IdentityUser
{
    public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
    {
//code removed for simplicity
    }
}

When I went to search where was that class, I found it in the website project inside a class located in: App_Start/IdentityConfig.cs

//...More code in the upper section
public class SmsService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        // Plug in your SMS service here to send a text message.
        return Task.FromResult(0);
    }
}

// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }
//More code bellow this...

Now, this brought me here because I'm really lost in the new ASP .NET Identity framework and I been struggling with really simple things that apparently aren't so simple.

How can I move the IdentityModel to another project without messing up my web site??

Some additional data:

  • Using VS 2013 Community
  • Using .NET Framework 4.5.1

回答1:

I successfully did this today after reading several posts that didn't seem to quite have all the pieces I needed. I hope this helps someone else.

My Goal: move models from existing web project into a different project. This includes domain models, business logic, and ASP Identity models.

[EDIT: Somehow I missed that the question was for Web Forms. I did this in MVC. However, I believe most would still hold true from what I'm seeing in a VS2013 web form project.]

Step by step:

Added new class library to solution named xyz.Models (xyz is the existing web project’s namespace) – using something else like ModelLib is fine, you’ll just have to search/replace namespaces later whereas the former you won't.

From the web project, move all domain models to the class library. I included the database context class (exam. XyzContext.cs), all AspNet... models, and the IdentityModels.cs. Note: leave microsoft's default ManageViewModels.cs where it is for the moment.

Next, I moved the ManageViewModels.cs into my web project’s ViewModels folder and changed it’s namespace from Models to ViewModels. The existing cshtml files in Views/Manage need to reflect this namespace change as well.

Next, ManageViewModels.cs is used by ManageController.cs so I added ‘using xyz.ViewModels’ to ManageController.cs.

Next, with an empty Models folder in my web project, I excluded it from the project.

Next, from the web project’s App_Start, I moved the IdentityConfig.cs to the models class library and changed it’s namespace to xyz.Models (also removed its ‘using xyz.Models’ statement)

Next, I added the class library (xyz.Models) as a reference to the web project.

Next, I installed the following NuGet Packages into the Class Library

  • Microsoft.AspNet.Identity.EntityFramework
  • Microsoft.AspNet.Identity.Owin
  • Microsoft.Owin (I just got the latest version from NuGet, which was slightly newer and forced me to update the existing reference on the web project – easy using NuGet’s Manage Packages > Update)

The following may not apply to your project, but these are other things I needed in the class library based on some business logic classes:

  • A reference to ‘System.Web.Mvc

  • A reference to ‘System.Web’ – Note: it was necessary to add a project reference to System.Web because I was using HttpContextBase, HttpContext in the class library (this was confusing at first since my class already had a ‘using System.Web’ statement. I won’t hash out why here, but just make sure you have ‘System.Web’ in your project references (System.Web.Mvc alone won’t do).

While at it, as my own preference, I changed “DefaultConnection“ in my IdentityModels.cs to the database context that I am using for my others (and deleted the reference in my web project's web.config for DefaultConnection; keeping “XyzContext”.) Note: all tables are in same db.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("XyzContext", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

At this point, compiling gave me a 'GetOwinContext' error in one of my custom classes that I had created for centralizing some aspnet identity business logic. To resolve this I needed another NuGet package in my class library: Microsoft.Owin.Host.SystemWeb.

All worked fine after that.