In the previous article we migrated configuration from an ASP.NET MVC 5 project to MVC 6. In this article, we migrate the registration, login, and user management features.
- This article covers the following topics:
- Configure Identity and Membership
- Migrate Registration and Login Logic
- Migrate User Management Features
Configure Identity and Membership
In ASP.NET MVC 5, authentication and identity features are configured in Startup.Auth.cs and IdentityConfig.cs, located in the App_Start folder. In MVC 6, these features are configured in Startup.cs. Before pulling in the required services and configuring them, we should add the required dependencies to the project. Open project.json and add “Microsoft.AspNet.Identity.EntityFramework” and “Microsoft.AspNet.Identity.Cookies” to the list of dependencies:"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-beta3",
"Microsoft.AspNet.Mvc": "6.0.0-beta3",
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta3",
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta3",
"Microsoft.AspNet.Security.Cookies": "1.0.0-beta3"
},
public void ConfigureServices(IServiceCollection services)
{
// Add EF services to the services container.
services.AddEntityFramework(Configuration)
.AddSqlServer()
.AddDbContext<ApplicationDbContext>();
// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>(Configuration)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc();
}
ApplicationUser.cs:
using Microsoft.AspNet.Identity;
namespace NewMvc6Project.Models
{
public class ApplicationUser : IdentityUser
{
}
}
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Data.Entity;
namespace NewMvc6Project.Models
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
private static bool _created = false;
public ApplicationDbContext()
{
// Create the database and schema if it doesn't exist
// This is a temporary workaround to create database until Entity Framework database migrations
// are supported in ASP.NET 5
if (!_created)
{
Database.AsMigrationsEnabled().ApplyMigrations();
_created = true;
}
}
protected override void OnConfiguring(DbContextOptions options)
{
options.UseSqlServer();
}
}
}
With these files in place, the Startup.cs file can be made to compile by updating its using statements:
using Microsoft.Framework.ConfigurationModel;
using Microsoft.AspNet.Hosting;
using NewMvc6Project.Models;
using Microsoft.AspNet.Identity;
Migrate Registration and Login Logic
With identity services configured for the application and data access configured using Entity Framework and SQL Server, we are now ready to add support for registration and login to the application. 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.Update _Layout.cshtml; uncomment the @Html.Partial line:
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
@*@Html.Partial("_LoginPartial")*@
</div>
</div>
Update _LoginPartial.cshtml with the following code (replace all of its contents):
@using System.Security.Principal
@if (User.Identity.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
}
}
else
{
<ul class="nav navbar-nav navbar-right">
<li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
Thanks for sharing this Informative post. Here describes about how to migrate the registration, login, and user management features. With ASP.NET MVC 5, authentication and identity features are configured in Startup. But in MVC 6, these features are configured in Startup.cs. Recently I got a blog about the purpose of getting the ASP.NET Hosting. Thanks a lot. Keep blogging like this.
ReplyDeleteThanks for sharing this.
ReplyDeleteOut of Gauge cargoes & Dangerous Goods transportation