充分利用具有n(3)层架构的MVC Owin身份
我一直在学习开箱即用的Owin Identity ,我喜欢它为我们提供用户管理的易用性。 然后我遇到的问题是它通过ApplicationDbContext
直接与EF(貌似)进行交互,这是我不想要的。 我更喜欢使用我的3层架构,IE它与服务层(BLL)交互,后者与EF交互。 我找不到模板,教程,甚至起点来维护所提供的所有function并实现我想要的分离。
那么有没有办法在MVC Identity包中使用服务层代替ApplicationDbContext
。
如果要使用现有数据库/表,则不必使用整个ASP.Net标识。 相反,您可以使用Owin Cookie身份validation中间件 。
我在GitHub上有代码示例。 如果要测试它,只需在AccountController.cs中设置一个断点,然后返回true。
以下是配置中间件和登录的两个主要类别。
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 claims = new List { 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); } }
- Asp.net身份entity framework数据库第一种方法有自己的表定义
- 微服务架构中的ASP.NET身份
- ‘Compare’是’System.ComponentModel.DataAnnotations.CompareAttribute’和’System.Web.Mvc.CompareAttribute’之间的模糊引用。
- CreateUserIdenityAsync返回自定义IdentityUser的“UserId not found”exception
- Store未实现IUserRoleStore ASP.NET Core 2.1 Identity
- 无法从AspNetUsers表中检索inheritance的自定义属性值
- UserManager错误 – 在上一个异步操作完成之前,在此上下文中启动了第二个操作
- ASP.net身份:如何获取当前的IdentityUser(ApplicationUser)? UserManager.FindById在哪里?
- ASP标识表列更改,但新列名称无法进行映射