[英]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移動到另一個項目?
一些額外的數據:
在閱讀了幾篇看起來並不完全具備我需要的文章之后,我今天成功地做到了這一點。 我希望這有助於其他人。
我的目標:將模型從現有的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包安裝到類庫中
以下內容可能不適用於您的項目,但這些是基於某些業務邏輯類在類庫中需要的其他內容:
對' System.Web.Mvc '的引用
對' System.Web '的引用 - 注意:有必要向System.Web添加一個項目引用,因為我在類庫中使用了HttpContextBase , HttpContext (由於我的類已經有了'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.