.NET Zone is brought to you in partnership with:

Imran is a DZone MVB and is not an employee of DZone and has posted 32 posts at DZone. View Full User Profile

A Sample of Onion Architecture with ASP.NET Identity

  • submit to reddit

I was working on a new ASP.NET web application. In this application, I was trying to use Onion Architecture. The application was also using ASP.NET Identity 2.0. Our goal was to create application that will easily replace (if required) the ASP.NET Identity system with any other Identity system or ASP.NET Identity Entity Framework store with any other Identity store without changing anything in our web application.  

We were able to create a web application without ASP.NET Identity and OWIN dependency using Onion architecture. I have created ASPNETIdentityWithOnion solution using Onion Architecture, because I thought it might be helpful for me and community in future. The web application in this solution contains Microsoft ASP.NET Identity Samples controllers(plus a ProductController) and views but without ASP.NET Identity and OWIN dependency

It might be helpful for users creating a new application using Onion and ASP.NET Identity. Existing applications can also take benefit from this solution. Here are some of benefits of using this solution,

  1. The web application uses latest ASP.NET MVC 5.1 framework which include user, role and account management(everything that included in Microsoft ASP.NET Identity Samples) but without ASP.NET Identity and OWIN dependency.
  2. Since there is no dependency on ASP.NET Identity and OWIN, unit testing and mocking of controller become very easy.
  3. The ASP.NET Identity implementation in the solution uses int as Id(identity column) as key instead of string.
  4. If you are looking to replace the Identity store in future without changing anything in the web application.
  5. You might want the ASP.NET Identity system might be replaced in future with different Identity system without changing anything in the web application.
  6. If you are creating a new web application with Onion architecture.
  7. The solution include a generic repository, generic unit of work and generic service with async and pagination support.
  8. The solution also include NLog which log all the EF queries.
  9. The web application will be only depend upon core project.
  10. and more... 

Please try/see/use the solution and let me know if you have any suggestion.

Published at DZone with permission of Imran Baloch, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Bharath Reddy replied on Thu, 2014/10/16 - 9:07pm

Hi Thank you fro your example It is excellent example but can you please explain How unit test write in this example.

Francis Austria replied on Sat, 2014/10/18 - 6:44am

I hope you can make a series of tutorials on how you ended up with the solution. It's a bit complicated for intermediate developers like me. Thanks.

Igor Goldobin replied on Tue, 2014/11/11 - 7:35pm

Imran. Good article. One improvement could be creating t4 templates to auto-generate you whole data access layer, as writing custom repositories can become a very tedious task in real world project. Something like http://www.sswdataonion.com would work.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.