Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过n(3)层体系结构充分利用MVC Owin身份_C#_Asp.net Mvc_Entity Framework_Asp.net Identity - Fatal编程技术网

C# 通过n(3)层体系结构充分利用MVC Owin身份

C# 通过n(3)层体系结构充分利用MVC Owin身份,c#,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net Mvc,Entity Framework,Asp.net Identity,我一直在开箱即用地学习,我喜欢它为我们提供的用户管理的易用性。我遇到的问题是,它通过ApplicationDbContext直接与EF交互(似乎),我不希望这样。我更愿意利用我的三层体系结构,即它与一个与EF交互的服务层(BLL)交互。我找不到一个模板、教程,甚至找不到一个起点来维护所提供的所有功能并实现我想要的分离 因此,有没有一种方法可以使用服务层来代替MVC标识包中的ApplicationDbContext。如果您想使用现有的数据库/表,您不必使用整个ASP.Net标识。相反,您可以只使用

我一直在开箱即用地学习,我喜欢它为我们提供的用户管理的易用性。我遇到的问题是,它通过
ApplicationDbContext
直接与EF交互(似乎),我不希望这样。我更愿意利用我的三层体系结构,即它与一个与EF交互的服务层(BLL)交互。我找不到一个模板、教程,甚至找不到一个起点来维护所提供的所有功能并实现我想要的分离


因此,有没有一种方法可以使用服务层来代替MVC标识包中的
ApplicationDbContext

如果您想使用现有的数据库/表,您不必使用整个ASP.Net标识。相反,您可以只使用Owin Cookie身份验证中间件

我有工作样本代码在。如果要测试它,只需将断点设置为,然后返回true

以下是配置中间件和登录的两个主要类

公共类OwinAuthenticationService:IAAuthenticationService
{
私有只读HttpContextBase\u上下文;
private const string AuthenticationType=“applicationcokie”;
public OwinAuthenticationService(HttpContextBase上下文)
{
_上下文=上下文;
}
公共无效登录(用户)
{
IList索赔=新清单
{
新索赔(ClaimTypes.Name、user.UserName),
新索赔(ClaimTypes.GivenName,user.FirstName),
新索赔(ClaimTypes.LastName、user.LastName),
};
索赔实体标识=新的索赔实体(索赔、认证类型);
IOwinContext上下文=_context.Request.GetOwinContext();
IAAuthenticationManager authenticationManager=context.Authentication;
authenticationManager.SignIn(标识);
}
公共无效签出()
{
IOwinContext上下文=_context.Request.GetOwinContext();
IAAuthenticationManager authenticationManager=context.Authentication;
authenticationManager.SignOut(AuthenticationType);
}
}

您想在自定义表中使用ASP.Net标识吗?@Win我想使用DAL层使用的数据库,使用表名
用户
,但我对所使用的列名没有意见。很酷,所以使用这个,我只需在
OwinAuthenticationService
中使用我的BLL即可?然后,我可以从那里添加注册、电子邮件验证、短信验证等内容。我使用的是个人帐户,这是我试图保持功能完整的原因,例如生成的
AccountController
,您将需要替换为基本上验证用户凭证并返回true或false的服务。
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);
    }
}