简体   繁体   English

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

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

I'm trying to move IdentityModels.cs to another project to keep the web site apart from the Data Access Layer. 我正在尝试将IdentityModels.cs移动到另一个项目,以使网站与数据访问层保持分离。

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

And also checked this question here: How to move MVC 5 IdentityModels.cs into a separate assembly 并且还在这里检查了这个问题: 如何将MVC 5 IdentityModels.cs移动到单独的程序集中

But I'm still confused because IdentityModels references another class called ApplicationUserManager as you can see bellow: 但我仍然感到困惑,因为IdentityModels引用另一个名为ApplicationUserManager的类,如下所示:

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

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 当我去搜索那个类的哪个地方时,我发现它位于一个类中的网站项目中: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...

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

How can I move the IdentityModel to another project without messing up my web site?? 如何在不弄乱我的网站的情况下将IdentityModel移动到另一个项目?

Some additional data: 一些额外的数据:

  • Using VS 2013 Community 使用VS 2013社区
  • Using .NET Framework 4.5.1 使用.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. 我希望这有助于其他人。

My Goal: move models from existing web project into a different project. 我的目标:将模型从现有的Web项目移动到另一个项目中。 This includes domain models, business logic, and ASP Identity models . 这包括域模型,业务逻辑和ASP身份模型

[ EDIT: Somehow I missed that the question was for Web Forms. [ 编辑:不知怎的,我错过了问题是Web表单。 I did this in MVC. 我是在MVC中做到的。 However, I believe most would still hold true from what I'm seeing in a VS2013 web form project.] 但是,我相信大部分内容仍然适用于我在VS2013网络表单项目中看到的内容。

Step by step: 一步步:

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

From the web project, move all domain models to the class library. 从Web项目中,将所有域模型移动到类库。 I included the database context class (exam. XyzContext.cs), all AspNet... models, and the IdentityModels.cs. 我包括数据库上下文类(考试.XyzContext.cs),所有AspNet ...模型和IdentityModels.cs。 Note: leave microsoft's default ManageViewModels.cs where it is for the moment. 注意: 暂时保留microsoft的默认ManageViewModels.cs

Next, I moved the ManageViewModels.cs into my web project's ViewModels folder and changed it's namespace from Models to ViewModels. 接下来,我将ManageViewModels.cs移动到我的Web项目的ViewModels文件夹中,并将其命名空间从Models更改为ViewModels。 The existing cshtml files in Views/Manage need to reflect this namespace change as well. Views / Manage中的现有cshtml文件也需要反映此命名空间更改。

Next, ManageViewModels.cs is used by ManageController.cs so I added 'using xyz.ViewModels' to ManageController.cs. 接下来,ManageController.cs使用ManageViewModels.cs,因此我将“使用xyz.ViewModels”添加到ManageController.cs。

Next, with an empty Models folder in my web project, I excluded it from the project. 接下来,在我的Web项目中有一个空的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) 接下来,从Web项目的App_Start,我将IdentityConfig.cs移动到模型类库并将其名称空间更改为xyz.Models (还删除了它的'using xyz.Models'语句)

Next, I added the class library (xyz.Models) as a reference to the web project. 接下来,我添加了类库(xyz.Models)作为Web项目的引用。

Next, I installed the following NuGet Packages into the Class Library 接下来,我将以下NuGet包安装到类库中

  • Microsoft.AspNet.Identity.EntityFramework Microsoft.AspNet.Identity.EntityFramework
  • Microsoft.AspNet.Identity.Owin Microsoft.AspNet.Identity.Owin
  • Microsoft.Owin (I just got the latest version from NuGet, which was slightly newer and forced me to update the existing reference on the web project – easy using NuGet's Manage Packages > Update) Microsoft.Owin (我刚刚从NuGet获得了最新版本,这个版本略微更新并且强迫我更新Web项目的现有参考 - 使用NuGet的管理软件包>更新很容易)

The following may not apply to your project, but these are other things I needed in the class library based on some business logic classes: 以下内容可能不适用于您的项目,但这些是基于某些业务逻辑类在类库中需要的其他内容:

  • A reference to ' System.Web.Mvc ' 对' System.Web.Mvc '的引用

  • A reference to ' System.Web ' – Note: it was necessary to add a project reference to System.Web because I was using HttpContextBase , HttpContext in the class library (this was confusing at first since my class already had a 'using System.Web' statement. I won't hash out why here, but just make sure you have 'System.Web' in your project references (System.Web.Mvc alone won't do). 对' System.Web '的引用 - 注意:有必要向System.Web添加一个项目引用,因为我在类库中使用了HttpContextBaseHttpContext (由于我的类已经有了'using System',所以起初很困惑。 Web'声明。我不会在这里讨论为什么,但只是确保你的项目引用中有'System.Web'(仅System.Web.Mvc不会)。

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 . 在此期间,我根据自己的偏好,将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();
    }
}

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. 此时,编译在我创建的一个自定义类中为我提供了一个“ GetOwinContext ”错误,用于集中某些aspnet标识业务逻辑。 To resolve this I needed another NuGet package in my class library: Microsoft.Owin.Host.SystemWeb . 要解决这个问题,我需要在我的类库中使用另一个NuGet包: Microsoft.Owin.Host.SystemWeb

All worked fine after that. 之后一切正常。

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

相关问题 在另一个项目中移动 Asp.net Identity 2 - Move the Asp.net Identity 2 in another project 如何将第三方dll从bin文件夹移动到ASP.NET Web Forms项目中的另一个文件夹? - How to move third party dlls from bin folder to another folder in ASP.NET Web Forms project? 在asp.net 4.5.1中使用Web表单中的标识和角色 - Using Identity and roles in web-forms in asp.net 4.5.1 无法将ASP.NET身份模型移动到另一个程序集中 - Unable to move ASP.NET Identity model into another assembly 将ASP.net Web表单项目复制到另一台服务器; using子句是正确的,但某些静态类无法识别 - Copied ASP.net web forms project to another server; using clauses are correct, but some static classes are not recognizable 如何将ASP.NET Web窗体应用程序从成员身份迁移到ASP.NET Identity 2.0? - How to migrate ASP.NET Web Forms application from Membership to ASP.NET Identity 2.0? 来自另一个DbContext的ASP.NET Web API身份属性 - ASP.NET Web API Identity property from another DbContext 在VisulaStudio 2017上创建和编辑ASP.net Web表单项目 - Create and Edit ASP.net web forms project on VisulaStudio 2017 将Startup.cs移动到类库(包)项目 - ASP.NET 5 - Move Startup.cs to Class Library (Package) Project - ASP.NET 5 将ASP.Net Web Forms项目部署到Fedora 24 - Deploying ASP.Net Web Forms project to Fedora 24
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM