c# – 充分利用具有n(3)层架构的MVC Owin身份
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 充分利用具有n(3)层架构的MVC Owin身份,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1724字,纯文字阅读大概需要3分钟。
内容图文
![c# – 充分利用具有n(3)层架构的MVC Owin身份](/upload/InfoBanner/zyjiaocheng/774/e0ecff005f874d38b81c5d2b6d20dfb8.jpg)
我一直在学习开箱即用的Owin Identity,我喜欢它为我们提供用户管理的易用性.然后我遇到的问题是它通过ApplicationDbContext直接与EF(貌似)进行交互,这是我不想要的.我更喜欢使用我的3层架构,IE它与服务层(BLL)交互,后者与EF交互.我找不到模板,教程,甚至起点来维护所提供的所有功能并实现我想要的分离.
那么有没有办法在MVC Identity包中使用服务层代替ApplicationDbContext.
解决方法:
如果要使用现有数据库/表,则不必使用整个ASP.Net标识.相反,您可以使用Owin Cookie身份验证中间件.
我在GitHub处有示例代码.如果要测试它,只需在AccountController.cs设置一个断点,然后返回true.
以下是配置中间件和登录的两个主要类别.
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "ApplicationCookie",
LoginPath = new PathString("/Account/Login")
});
}
}
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);
}
}
内容总结
以上是互联网集市为您收集整理的c# – 充分利用具有n(3)层架构的MVC Owin身份全部内容,希望文章能够帮你解决c# – 充分利用具有n(3)层架构的MVC Owin身份所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。