Fully utilizing MVC Owin identity with n(3)-tier a

2019-02-23 19:10发布

问题:

I've been learning out of the box Owin Identity and I love the ease of use it provides us with user management. Then problem that I have is that it interacts directly with EF (seemingly) via ApplicationDbContext which I don't want. I would prefer to utilize my 3 tier architecture, IE it interacts with a service layer (BLL) which interacts with EF. I can't find a template, tutorial, or even starting point to maintain all the functionality that is provided and achieve the separation I want.

So is there a way to use a service layer in place of the ApplicationDbContext in MVC Identity package.

回答1:

If you want to use existing database/tables, you do not have to use entire ASP.Net Identity. Instead, you can just use Owin Cookie Authentication middleware.

I have working sample code at GitHub. If you want to test it, you just set a break-point at AccountController.cs, and return true.

The followings are two main classes of configure the middleware and sign-in.

Startup.cs

public class Startup
{
   public void Configuration(IAppBuilder app)
   {
      app.UseCookieAuthentication(new CookieAuthenticationOptions
      {
        AuthenticationType = "ApplicationCookie",
        LoginPath = new PathString("/Account/Login")
      });
   }
}

OwinAuthenticationService.cs

public class OwinAuthenticationService : IAuthenticationService
{
    private readonly HttpContextBase _context;
    private const string AuthenticationType = "ApplicationCookie";

    public OwinAuthenticationService(HttpContextBase context)
    {
        _context = context;
    }

    public void SignIn(User user)
    {
        IList<Claim> claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim(ClaimTypes.GivenName, user.FirstName),
                new Claim(ClaimTypes.Surname, user.LastName),
            };

        ClaimsIdentity identity = new ClaimsIdentity(claims, AuthenticationType);

        IOwinContext context = _context.Request.GetOwinContext();
        IAuthenticationManager authenticationManager = context.Authentication;

        authenticationManager.SignIn(identity);
    }

    public void SignOut()
    {
        IOwinContext context = _context.Request.GetOwinContext();
        IAuthenticationManager authenticationManager = context.Authentication;

        authenticationManager.SignOut(AuthenticationType);
    }
}