移动到另一个项目

移动到另一个项目

本文介绍了ASP.NET Web 窗体和标识:将 IdentityModels.cs 移动到另一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 IdentityModels.cs 移至另一个项目,以使网站与数据访问层分开.

I'm trying to move IdentityModels.cs to another project to keep the web site apart from the Data Access Layer.

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

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

And also checked this question here: How to move MVC 5 IdentityModels.cs into a separate assembly

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

But I'm still confused because IdentityModels references another class called ApplicationUserManager as you can see bellow:

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

当我去搜索那个类在哪里时,我在网站项目中的一个类中找到了它:App_Start/IdentityConfig.cs

When I went to search where was that class, I found it in the website project inside a class located in: 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 框架中,而且我一直在努力处理那些显然不那么简单的非常简单的事情.

Now, this brought me here because I'm really lost in the new ASP .NET Identity framework and I been struggling with really simple things that apparently aren't so simple.

如何将 IdentityModel 移动到另一个项目而不破坏我的网站?

How can I move the IdentityModel to another project without messing up my web site??

一些额外的数据:

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

推荐答案

在阅读了几篇似乎没有我需要的所有部分的帖子后,我今天成功地做到了这一点.我希望这对其他人有帮助.

I successfully did this today after reading several posts that didn't seem to quite have all the pieces I needed. I hope this helps someone else.

我的目标:将模型从现有的网络项目移动到不同的项目中.这包括域模型、业务逻辑和 ASP 身份模型.

My Goal: move models from existing web project into a different project. This includes domain models, business logic, and ASP Identity models.

[不知何故,我错过了这个问题是针对 Web 表单的.我在MVC中做到了这一点.但是,我相信大多数仍然适用于我在 VS2013 网络表单项目中看到的内容.]

[ Somehow I missed that the question was for Web Forms. I did this in MVC. However, I believe most would still hold true from what I'm seeing in a VS2013 web form project.]

循序渐进:

向名为 xyz.Models 的解决方案添加了新的类库(xyz 是现有 Web 项目的命名空间)——使用 ModelLib 之类的其他东西很好,你只需要稍后搜索/替换命名空间,而前者你赢了't.

Added new class library to solution named xyz.Models (xyz is the existing web project’s namespace) – using something else like ModelLib is fine, you’ll just have to search/replace namespaces later whereas the former you won't.

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

From the web project, move all domain models to the class library. I included the database context class (exam. XyzContext.cs), all AspNet... models, and the IdentityModels.cs. Note: leave microsoft's default ManageViewModels.cs where it is for the moment.

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

Next, I moved the ManageViewModels.cs into my web project’s ViewModels folder and changed it’s namespace from Models to ViewModels. The existing cshtml files in Views/Manage need to reflect this namespace change as well.

接下来,ManageController.cs 使用了 ManageViewModels.cs,所以我在 ManageController.cs 中添加了using xyz.ViewModels".

Next, ManageViewModels.cs is used by ManageController.cs so I added ‘using xyz.ViewModels’ to ManageController.cs.

接下来,在我的 web 项目中有一个空的 Models 文件夹,我将它从项目中排除.

Next, with an empty Models folder in my web project, I excluded it from the project.

接下来,从 Web 项目的 App_Start,我将 IdentityConfig.cs 移动到模型类库并将其命名空间更改为 xyz.Models (也删除了它的使用 xyz.Models"声明)

Next, from the web project’s App_Start, I moved the IdentityConfig.cs to the models class library and changed it’s namespace to xyz.Models (also removed its ‘using xyz.Models’ statement)

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

Next, I added the class library (xyz.Models) as a reference to the web project.

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

Next, I installed the following NuGet Packages into the Class Library

  • Microsoft.AspNet.Identity.EntityFramework
  • Microsoft.AspNet.Identity.Owin
  • Microsoft.Owin (我刚刚从 NuGet 获得了最新版本,这是稍微更新,迫使我更新现有的参考web 项目 - 使用 NuGet 的管理包轻松 > 更新)

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

  • System.Web.Mvc"的引用

System.Web"的引用——注意:需要添加一个项目引用 System.Web 因为我使用的是 HttpContextBaseHttpContext 在类库中(这起初令人困惑,因为我的班级已经有了使用 System.Web"的声明.我不会散列为什么在这里,但只要确保你的项目中有System.Web"引用(仅 System.Web.Mvc 不行).

A reference to ‘System.Web’ – Note: it was necessary to add a projectreference to System.Web because I was using HttpContextBase,HttpContext in the class library (this was confusing at first sincemy class already had a ‘using System.Web’ statement. I won’t hash outwhy here, but just make sure you have ‘System.Web’ in your projectreferences (System.Web.Mvc alone won’t do).

同时,根据我自己的喜好,我将 IdentityModels.cs 中的DefaultConnection"更改为我用于其他人的数据库上下文(并删除了我的 web 项目的 web.config 中对 DefaultConnection 的引用; 保持XyzContext".)注意:所有表都在同一个数据库中.

While at it, as my own preference, I changed "DefaultConnection" in my IdentityModels.cs to the database context that I am using for my others (and deleted the reference in my web project's web.config for DefaultConnection; keeping "XyzContext".) Note: all tables are in same db.

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

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

此时,在我为集中某些 aspnet 身份业务逻辑而创建的自定义类之一中,编译给了我一个GetOwinContext"错误.为了解决这个问题,我需要在我的类库中使用另一个 NuGet 包:Microsoft.Owin.Host.SystemWeb.

At this point, compiling gave me a 'GetOwinContext' error in one of my custom classes that I had created for centralizing some aspnet identity business logic. To resolve this I needed another NuGet package in my class library: Microsoft.Owin.Host.SystemWeb.

之后一切正常.

这篇关于ASP.NET Web 窗体和标识:将 IdentityModels.cs 移动到另一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 20:58