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
并且还在这里检查了这个问题:How to move MVC 5 IdentityModels.cs into a separate assembly
但我仍然感到困惑,因为 IdentityModels 引用了另一个名为 ApplicationUserManager 的 class,如下所示:
public class ApplicationUser : IdentityUser
{
public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
{
//code removed for simplicity
}
}
当我去搜索 class 的位置时,我在 class 的网站项目中找到了它,位于: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 Web 表单项目中看到的内容。]
一步一步:
向名为 xyz.Models 的解决方案添加了新的 class 库(xyz 是现有 Web 项目的命名空间)– 使用其他类似 ModelLib 的东西很好,你只需要search/replace 后命名空间而前者则不会。
从 Web 项目中,将所有领域模型移动到 class 库中。我包括了数据库上下文 class(考试 XyzContext.cs)、所有 AspNet... 模型和 IdentityModels.cs。 注意:暂时保留微软的默认 ManageViewModels.cs。
接下来,我将 ManageViewModels.cs 移动到我的 Web 项目的 ViewModels 文件夹中,并将其命名空间从 Models 更改为 ViewModels。 Views/Manage 中的现有 cshtml 文件也需要反映此名称空间更改。
接下来,ManageViewModels.cs 被 ManageController.cs 使用,所以我在 ManageController.cs 中添加了“使用 xyz.ViewModels”。
接下来,我的 Web 项目中有一个空的 Models 文件夹,我将其从项目中排除。
接下来,我将 IdentityConfig.cs 从 Web 项目的 App_Start 移动到模型 class 库并将其命名空间更改为 xyz.Models (也删除了它的'using xyz.Models'声明)
接下来,我添加了 class 库 (xyz.Models) 作为对 Web 项目的引用。
接下来,我将以下 NuGet 包安装到 Class 库
- Microsoft.AspNet.Identity.EntityFramework
- Microsoft.AspNet.Identity.Owin
- Microsoft.Owin (我刚刚从 NuGet 得到了最新版本,它是
稍微更新一点,迫使我更新现有的参考资料
web 项目 – 轻松使用 NuGet 的管理包 > 更新)
以下内容可能不适用于您的项目,但这些是我根据一些业务逻辑在 class 库中需要的其他内容 classes:
对“System.Web.Mvc”的引用
对“System.Web”的引用 – 注意:必须添加一个项目
引用 System.Web 因为我使用的是 HttpContextBase,
HttpContext 在 class 库中(这起初令人困惑,因为
我的 class 已经有一个“using System.Web”声明。我不会散列
为什么在这里,但只要确保你的项目中有“System.Web”
参考文献(单独使用 System.Web.Mvc 是不行的)。
同时,根据我自己的喜好,我将 IdentityModels.cs 中的“DefaultConnection”更改为我为其他人使用的数据库上下文(并删除了我网站中的引用项目的 web.config 用于 DefaultConnection;保留“XyzContext”。)注意:所有表都在同一个数据库中。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("XyzContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
此时,编译在我为集中某些 aspnet 身份业务逻辑而创建的自定义 classes 之一中出现了“GetOwinContext”错误。为了解决这个问题,我的 class 库中需要另一个 NuGet 包:Microsoft.Owin.Host.SystemWeb.
之后一切正常。
我正在尝试将 IdentityModels.cs 移动到另一个项目以使网站与数据访问层分开。
我遵循了这个教程:http://blog.rebuildall.net/2013/10/22/Moving_ASP_NET_Identity_model_into_another_assembly
并且还在这里检查了这个问题:How to move MVC 5 IdentityModels.cs into a separate assembly
但我仍然感到困惑,因为 IdentityModels 引用了另一个名为 ApplicationUserManager 的 class,如下所示:
public class ApplicationUser : IdentityUser
{
public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
{
//code removed for simplicity
}
}
当我去搜索 class 的位置时,我在 class 的网站项目中找到了它,位于: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 Web 表单项目中看到的内容。]
一步一步:
向名为 xyz.Models 的解决方案添加了新的 class 库(xyz 是现有 Web 项目的命名空间)– 使用其他类似 ModelLib 的东西很好,你只需要search/replace 后命名空间而前者则不会。
从 Web 项目中,将所有领域模型移动到 class 库中。我包括了数据库上下文 class(考试 XyzContext.cs)、所有 AspNet... 模型和 IdentityModels.cs。 注意:暂时保留微软的默认 ManageViewModels.cs。
接下来,我将 ManageViewModels.cs 移动到我的 Web 项目的 ViewModels 文件夹中,并将其命名空间从 Models 更改为 ViewModels。 Views/Manage 中的现有 cshtml 文件也需要反映此名称空间更改。
接下来,ManageViewModels.cs 被 ManageController.cs 使用,所以我在 ManageController.cs 中添加了“使用 xyz.ViewModels”。
接下来,我的 Web 项目中有一个空的 Models 文件夹,我将其从项目中排除。
接下来,我将 IdentityConfig.cs 从 Web 项目的 App_Start 移动到模型 class 库并将其命名空间更改为 xyz.Models (也删除了它的'using xyz.Models'声明)
接下来,我添加了 class 库 (xyz.Models) 作为对 Web 项目的引用。
接下来,我将以下 NuGet 包安装到 Class 库
- Microsoft.AspNet.Identity.EntityFramework
- Microsoft.AspNet.Identity.Owin
- Microsoft.Owin (我刚刚从 NuGet 得到了最新版本,它是 稍微更新一点,迫使我更新现有的参考资料 web 项目 – 轻松使用 NuGet 的管理包 > 更新)
以下内容可能不适用于您的项目,但这些是我根据一些业务逻辑在 class 库中需要的其他内容 classes:
对“System.Web.Mvc”的引用
对“System.Web”的引用 – 注意:必须添加一个项目 引用 System.Web 因为我使用的是 HttpContextBase, HttpContext 在 class 库中(这起初令人困惑,因为 我的 class 已经有一个“using System.Web”声明。我不会散列 为什么在这里,但只要确保你的项目中有“System.Web” 参考文献(单独使用 System.Web.Mvc 是不行的)。
同时,根据我自己的喜好,我将 IdentityModels.cs 中的“DefaultConnection”更改为我为其他人使用的数据库上下文(并删除了我网站中的引用项目的 web.config 用于 DefaultConnection;保留“XyzContext”。)注意:所有表都在同一个数据库中。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("XyzContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
此时,编译在我为集中某些 aspnet 身份业务逻辑而创建的自定义 classes 之一中出现了“GetOwinContext”错误。为了解决这个问题,我的 class 库中需要另一个 NuGet 包:Microsoft.Owin.Host.SystemWeb.
之后一切正常。