简体   繁体   English

充分利用具有n(3)层架构的MVC Owin身份

[英]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. 我一直在学习开箱即用的Owin Identity ,我喜欢它为我们提供用户管理的易用性。 Then problem that I have is that it interacts directly with EF (seemingly) via ApplicationDbContext which I don't want. 然后我遇到的问题是它通过ApplicationDbContext直接与EF(貌似)进行交互,这是我不想要的。 I would prefer to utilize my 3 tier architecture, IE it interacts with a service layer (BLL) which interacts with EF. 我更喜欢使用我的3层架构,IE它与服务层(BLL)交互,后者与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. 那么有没有办法在MVC Identity包中使用服务层代替ApplicationDbContext

If you want to use existing database/tables, you do not have to use entire ASP.Net Identity. 如果要使用现有数据库/表,则不必使用整个ASP.Net标识。 Instead, you can just use Owin Cookie Authentication middleware . 相反,您可以使用Owin Cookie身份验证中间件

I have working sample code at GitHub . 我在GitHub上有代码示例。 If you want to test it, you just set a break-point at AccountController.cs , and return true. 如果要测试它,只需在AccountController.cs中设置一个断点,然后返回true。

The followings are two main classes of configure the middleware and sign-in. 以下是配置中间件和登录的两个主要类别。

Startup.cs Startup.cs

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

OwinAuthenticationService.cs 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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