認証および Identity ASP.NET Core への移行Migrate Authentication and Identity to ASP.NET Core
作成者: Steve SmithBy Steve Smith
前の記事では、 ASP.NET mvc プロジェクトから ASP.NET CORE mvc に構成を移行しています。In the previous article, we migrated configuration from an ASP.NET MVC project to ASP.NET Core MVC. この記事では、登録、ログイン、およびユーザー管理機能を移行します。In this article, we migrate the registration, login, and user management features.
Identityメンバーシップを構成するConfigure Identity and Membership
ASP.NET MVC では、認証と id 機能は、[ Identity App_Start ] フォルダーにある Startup.Auth.cs と Identity Config.cs の ASP.NET を使用して構成されます。In ASP.NET MVC, authentication and identity features are configured using ASP.NET Identity in Startup.Auth.cs and IdentityConfig.cs , located in the App_Start folder. ASP.NET Core MVC では、これらの機能は Startup.cs で構成されています。In ASP.NET Core MVC, these features are configured in Startup.cs .
次の NuGet パッケージをインストールします。Install the following NuGet packages:
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.AspNetCore.Authentication.Cookies
Microsoft.EntityFrameworkCore.SqlServer
Startup.cs で、 Startup.ConfigureServices
Entity Framework とサービスを使用するようにメソッドを更新し Identity ます。In Startup.cs , update the Startup.ConfigureServices
method to use Entity Framework and Identity services:
public void ConfigureServices(IServiceCollection services)
{
// Add EF services to the services container.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
}
この時点で、上記のコードでは、ASP.NET MVC プロジェクトからまだ移行していない2つの型が参照されています。 ApplicationDbContext
ApplicationUser
At this point, there are two types referenced in the above code that we haven't yet migrated from the ASP.NET MVC project: ApplicationDbContext
and ApplicationUser
. ASP.NET Core プロジェクトに新しい モデル フォルダーを作成し、これらの型に対応する2つのクラスを追加します。Create a new Models folder in the ASP.NET Core project, and add two classes to it corresponding to these types. これらのクラスの ASP.NET MVC バージョンは /Models/ Identity Models.cs にありますが、移行されたプロジェクトのクラスごとに1つのファイルを使用します。これはより明確であるためです。You will find the ASP.NET MVC versions of these classes in /Models/IdentityModels.cs , but we will use one file per class in the migrated project since that's more clear.
ApplicationUser.cs :ApplicationUser.cs :
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
namespace NewMvcProject.Models
{
public class ApplicationUser : IdentityUser
{
}
}
ApplicationDbContext.cs :ApplicationDbContext.cs :
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.Data.Entity;
namespace NewMvcProject.Models
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Core Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Core Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
}
ASP.NET Core MVC Starter Web プロジェクトには、ユーザーまたはの多くのカスタマイズが含まれていません ApplicationDbContext
。The ASP.NET Core MVC Starter Web project doesn't include much customization of users, or the ApplicationDbContext
. 実際のアプリを移行する場合は、アプリのユーザーとクラスのすべてのカスタムプロパティとメソッド、および DbContext
アプリが使用するその他のモデルクラスも移行する必要があります。When migrating a real app, you also need to migrate all of the custom properties and methods of your app's user and DbContext
classes, as well as any other Model classes your app utilizes. たとえば、にがある場合は、 DbContext
DbSet<Album>
クラスを移行する必要があり Album
ます。For example, if your DbContext
has a DbSet<Album>
, you need to migrate the Album
class.
これらのファイルを配置したら、ステートメントを更新して、 Startup.cs ファイルをコンパイルすることができ using
ます。With these files in place, the Startup.cs file can be made to compile by updating its using
statements:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
これで、アプリは認証とサービスをサポートする準備ができました Identity 。Our app is now ready to support authentication and Identity services. これらの機能をユーザーに公開する必要があるだけです。It just needs to have these features exposed to users.
登録とログインのロジックを移行するMigrate registration and login logic
IdentityEntity Framework と SQL Server を使用して構成されたアプリとデータアクセス用に構成されたサービスを使用して、登録とアプリへのログインのサポートを追加する準備が整いました。With Identity services configured for the app and data access configured using Entity Framework and SQL Server, we're ready to add support for registration and login to the app. 移行プロセスの前半で、 _Layout の _LoginPartial への参照がコメントアウトされていることを思い出してください。Recall that earlier in the migration process we commented out a reference to _LoginPartial in _Layout.cshtml . 次に、そのコードに戻り、コメントを解除し、ログイン機能をサポートするために必要なコントローラーとビューを追加します。Now it's time to return to that code, uncomment it, and add in the necessary controllers and views to support login functionality.
_Layout の行のコメントを解除 @Html.Partial
します。Uncomment the @Html.Partial
line in _Layout.cshtml :
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
@*@Html.Partial("_LoginPartial")*@
</div>
</div>
次に、 Razor _LoginPartial という名前の新しいビューを ビュー/共有 フォルダーに追加します。Now, add a new Razor view called _LoginPartial to the Views/Shared folder:
次のコードを使用して _LoginPartial を更新 します (すべての内容を置き換えます)。Update _LoginPartial.cshtml with the following code (replace all of its contents):
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
@if (SignInManager.IsSignedIn(User))
{
<form asp-area="" asp-controller="Account" asp-action="Logout" method="post" id="logoutForm" class="navbar-right">
<ul class="nav navbar-nav navbar-right">
<li>
<a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
</li>
<li>
<button type="submit" class="btn btn-link navbar-btn navbar-link">Log out</button>
</li>
</ul>
</form>
}
else
{
<ul class="nav navbar-nav navbar-right">
<li><a asp-area="" asp-controller="Account" asp-action="Register">Register</a></li>
<li><a asp-area="" asp-controller="Account" asp-action="Login">Log in</a></li>
</ul>
}
この時点で、ブラウザーでサイトを最新の状態に更新できるようになります。At this point, you should be able to refresh the site in your browser.
まとめSummary
ASP.NET Core には、ASP.NET 機能の変更が導入さ Identity れています。ASP.NET Core introduces changes to the ASP.NET Identity features. この記事では、ASP.NET の認証およびユーザー管理機能を ASP.NET Core に移行する方法について説明しました Identity 。In this article, you have seen how to migrate the authentication and user management features of ASP.NET Identity to ASP.NET Core.