Identity 2.0.0 on the MVC5 Architecture Board

I am trying to integrate the recently released ASP.NET Identity 2.0.0 into a three-layer MVC application. I'm not sure I will go in the right direction. I see two approaches.

In the first approach, I went with integrating Identity into each logical level. Having some technical problems integrating but still developing.

In the second approach, go to a standalone encapsulated assembly designed to provide security.

At the moment I went with approach 1, but I asked all this. Also, any other approaches?


Approach 1

Web
Startup.cs
/App_Start/Startup.Auth
/Controllers/Account
/Controllers/Manage
/Controllers/RolesAdmin
/Controllers/UserAdmin
/ViewModels
/Views

Business Logic
/Service/AccountService
/Service/ApplicationRoleManager
/Service/ApplicationUserManager
/Service/EmailService
/Service/SmsService
/Service/SignInHelper

Data
ApplicationDbContext
ApplicationUser

, Identity , , . -, , "" , .

: , Web/App_Start/Startup.Auth Busness Logic

app.CreatePerOwinContext(ApplicationDbContext.Create);

. . ( , , ).


2

, , .. Identity 2.0.0 . . , . . ( ) , . .

+4
1

1, , HttpContext.Current. [ "DbActiveContext" ] , , , . , idleity aspnet . , , , , , , .

namespace Data.Common
{
    public class ConnectionHelper : IConnectionHelper
    {
        private ApplicationDbContext _context;

        public ApplicationDbContext Context
        {
            get
            {
                 if (_context == null && HttpContext.Current.Items["DbActiveContext"] != null)
                {
                    _context = (ApplicationDbContext)HttpContext.Current.Items["DbActiveContext"];
                }
                else if (_context == null && HttpContext.Current.Items["DbActiveContext"] == null)
                {
                    _context = new ApplicationDbContext();
                    HttpContext.Current.Items.Add("DbActiveContext", _context);
                }
                return _context;
            }
            set { _context = value; }
        }
    }
}

, usermanager DI, - :

 public UserController()
         : this(
             new ApplicationUserManager(new UserStore<ApplicationUser>(new ConnectionHelper().Context)),
             new UserService())
    {

    }

UserService, :

public class UserService
{
    private readonly IRepository<ApplicationUser> _user;
    private readonly UserManager<ApplicationUser> _userManager;

    public UserService(IRepository<ApplicationUser> user,
        UserManager<ApplicationUser> userManager)
    {
        _user = user;
        _userManager = userManager;

    }

    public UserService()
        : this(
            new Repository<ApplicationUser>(new ConnectionHelper()),
            new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new   ConnectionHelper().Context)))
    {

    }

, !

+2

Source: https://habr.com/ru/post/1540447/


All Articles