繁体   English   中英

ASP.NET Web窗体和标识:将IdentityModels.cs移动到另一个项目

[英]ASP.NET Web Forms and Identity: Move IdentityModels.cs to another project

我正在尝试将IdentityModels.cs移动到另一个项目,以使网站与数据访问层保持分离。

我遵循了这个教程: http//blog.rebuildall.net/2013/10/22/Moving_ASP_NET_Identity_model_into_another_assembly

并且还在这里检查了这个问题: 如何将MVC 5 IdentityModels.cs移动到单独的程序集中

但我仍然感到困惑,因为IdentityModels引用另一个名为ApplicationUserManager的类,如下所示:

 public class ApplicationUser : IdentityUser
{
    public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
    {
//code removed for simplicity
    }
}

当我去搜索那个类的哪个地方时,我发现它位于一个类中的网站项目中:App_Start / IdentityConfig.cs

//...More code in the upper section
public class SmsService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        // Plug in your SMS service here to send a text message.
        return Task.FromResult(0);
    }
}

// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }
//More code bellow this...

现在,这让我来到这里,因为我真的迷失在新的ASP .NET Identity框架中,而且我一直在努力处理显然不那么简单的非常简单的事情。

如何在不弄乱我的网站的情况下将IdentityModel移动到另一个项目?

一些额外的数据:

  • 使用VS 2013社区
  • 使用.NET Framework 4.5.1

在阅读了几篇看起来并不完全具备我需要的文章之后,我今天成功地做到了这一点。 我希望这有助于其他人。

我的目标:将模型从现有的Web项目移动到另一个项目中。 这包括域模型,业务逻辑和ASP身份模型

[ 编辑:不知怎的,我错过了问题是Web表单。 我是在MVC中做到的。 但是,我相信大部分内容仍然适用于我在VS2013网络表单项目中看到的内容。

一步步:

为名为xyz.Models的解决方案添加了新的类库(xyz是现有的Web项目的命名空间) - 使用像ModelLib这样的其他东西很好,你只需要稍后搜索/替换名称空间,而不是前者。

从Web项目中,将所有域模型移动到类库。 我包括数据库上下文类(考试.XyzContext.cs),所有AspNet ...模型和IdentityModels.cs。 注意: 暂时保留microsoft的默认ManageViewModels.cs

接下来,我将ManageViewModels.cs移动到我的Web项目的ViewModels文件夹中,并将其命名空间从Models更改为ViewModels。 Views / Manage中的现有cshtml文件也需要反映此命名空间更改。

接下来,ManageController.cs使用ManageViewModels.cs,因此我将“使用xyz.ViewModels”添加到ManageController.cs。

接下来,在我的Web项目中有一个空的Models文件夹,我将其从项目中排除。

接下来,从Web项目的App_Start,我将IdentityConfig.cs移动到模型类库并将其名称空间更改为xyz.Models (还删除了它的'using xyz.Models'语句)

接下来,我添加了类库(xyz.Models)作为Web项目的引用。

接下来,我将以下NuGet包安装到类库中

  • Microsoft.AspNet.Identity.EntityFramework
  • Microsoft.AspNet.Identity.Owin
  • Microsoft.Owin (我刚刚从NuGet获得了最新版本,这个版本略微更新并且强迫我更新Web项目的现有参考 - 使用NuGet的管理软件包>更新很容易)

以下内容可能不适用于您的项目,但这些是基于某些业务逻辑类在类库中需要的其他内容:

  • 对' System.Web.Mvc '的引用

  • 对' System.Web '的引用 - 注意:有必要向System.Web添加一个项目引用,因为我在类库中使用了HttpContextBaseHttpContext (由于我的类已经有了'using System',所以起初很困惑。 Web'声明。我不会在这里讨论为什么,但只是确保你的项目引用中有'System.Web'(仅System.Web.Mvc不会)。

在此期间,我根据自己的偏好,将IdentityModels.cs中的“DefaultConnection”更改为我用于其他人的数据库上下文( 并删除了我的web项目的web.config中的DefaultConnection引用;保留了“XyzContext”。 )注意:所有表都在同一个db中

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("XyzContext", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

此时,编译在我创建的一个自定义类中为我提供了一个“ GetOwinContext ”错误,用于集中某些aspnet标识业务逻辑。 要解决这个问题,我需要在我的类库中使用另一个NuGet包: Microsoft.Owin.Host.SystemWeb

之后一切正常。

暂无
暂无

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

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