简体   繁体   中英

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

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.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM