Question

Setup:

To enforce separation of concerns, I put my services in a separate library project to my MVC 5 project, and my repositories in a separate Repositories project. As a result, I have a "MembershipRepository" that basically holds the code to use the Membership provider. Not sure if the best design, but it works for me and provides my various services with a "Repository" that they can use for any Membership stuff.

The application I am working on (or rather updating), just needs to store logins and passwords, list users, find what roles they are in etc, all using a SQL Server database. No fancy stuff.

The interface for my MembershipRepository is (translated to equivalent classes for ASP.NET Identity):

public interface IMembershipRepository
{
    IEnumerable<AspNetUser> GetAllUsers();
    AspNetUser GetUser(string userName);
    string GetUserEmail(string userName);
    string[] GetRoles(string userName);
    bool IsUserInRole(string userName, string roleName);
    void RemoveUserFromRole(string userName, string roleName);
    void AddUserToRole(string userName, string roleName);
    void UpdateUser(AspNetUser user);
    void CreateUser(string userName, string password, string email, bool isApproved, out CreateStatus createStatus);
    void DeleteUser(string userName);
}

Problem:

Implementing IMembershipRepository in the days of ASP.NET Membership was dead easy in my Repositories library, as I could get a reference to System.Web.Security in there, but now, with ASP.NET Identity, the various Microsoft.AspNet.Identity references (EntityFramework, Core, Owin, etc) are not available to my library. I presume I have to install the relevant Nuget packages, so these are my questions:

Questions:

Should I:

  1. Install the Nuget packages in my library, and use ASP.NET Identity there?

  2. If so, what packages should I install for basic working with the SQL Server db? I presume Microsoft.AspNet.Identity.Core and Microsoft.AspNet.Identity.EntityFramework, but I might be proven wrong.

  3. Will this setup work, from the repositories project? Or does ASP.NET Identity only work in the MVC project? The standard project comes with everything jumbled into one project, ie, no such separation of concerns as I try to implement.

Sorry if the question is basic, but I have been off sick for a while, and ASP.NET Identity has happened in the meantime.

Edit:

I would have to move the IdentityModels.cs file into the Models project that is accessible to the Repositories project.

Question: would the migrations still work from there? Can't see why not, but as I am asking...

Was it helpful?

Solution

I have gone ahead and just done as I was semi suggesting in my questions. It works, but with some issues:

Issues:

A. I installed the Microsoft.AspNet.Identity.[various] Nuget packages in the library projects as required. However, this immediately created a problem, as the Nuget packages installed version 2, and in the MVC project, version 1 was installed when creating the new project. Uninstalling in the MVC project and reinstalling the version 2 Microsoft.AspNet.Identity.[various] packets solved this issue.

B. As a result of A, the tables in the database were now out of synch with ASP.NET Identity 2 tables, so you get loads of invalid column errors when trying to do something. As this is a test project, I just deleted the Identity tables from the database, removed the migrations folders and ran the project and tried to register a user. This resulted in the tables being re created in the db. However...

C. The AspNetUser table has a datetime field, that produced the following error:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.

The solution to this was to manually change any datetime field in the AspNetUser table to datetime2. Not sure why, but presumably AspNet Identity 2 is creating default values for the date fields that are out of range.

After manually changing the fields in the table in SSMA, I can register a user, login and logoff. So all is working.

Answers to my own questions:

Following the numbering of my Questions in my original SO Question:

  1. Yes, you should install the Nuget Packages in whichever library you will be using ASP.NET Identity from. You may have issues with a version clash between the Nuget packages used and what was installed when your MVC project was created as described above.

  2. To work with just SQL Server, you need to install all three packages, ie, Microsoft.AspNet.Identity.Core, Microsoft.AspNet.Identity.EntityFramework, and Microsoft.AspNet.Identity.OWin. I probably needed the last package (Microsoft.AspNet.Identity.Owin) because I did not edit the auto generated AccountController, that I suspect references owin stuff in case you want to use that.

  3. Yes, it does work in a library project.

Edit question: Yes, but you will have to enable Migrations for that project (I did it using the package manager console command: Enable-Migrations).

Anyway, that answers my own question quite comprehensively. I would still appreciate any clarification that you feel is appropriate.

MAJOR EDIT IF WORKING WITH ASP.NET IDENTITY 2.0 (time of writing: 28 April 2014)!!!:

Although the above will work, to some degree, there is the issue that you are not getting the full benefit of ASP.NET Identity 2.0. This is because, at the time of writing (April 2014), a new MVC 5 project is created using ASP.NET Identity 1.0.

To get ASP.NET Identity 2.0 in this scenario, the best option is to create a new project using the Empty template, then use the ASP.NET Identity 2.0 sample Nuget package, then move any existing work into this project / solution.

Finally, you move the various bits of ASP.NET Identity to the class libraries where you want them in accordance with your separation of concerns design. It is a bit of extra work, but it does work.

I added this edit because I didn't want anyone (as dumb as me) going off and then saying "Hang on!" when they get clashes between libraries, etc.

Also, the samples come with some extra functionality that is useful, in that it replaces the ASP.NET project configuration tool.

HTH.

OTHER TIPS

You can find an example of separating out ASP.NET Identity and the use of repositories in the SimpleSecurity project. There is a reference application and you can find the source code here. This implementation uses ASP.NET Identity 2.0. You can find discussion on this implementation in this blog.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top