From b4b3eae8bb3d18ccfd34996ae3bedeb729022db3 Mon Sep 17 00:00:00 2001 From: TOCANE TECHNOLOGIES Date: Sat, 6 Jan 2018 15:07:49 +0100 Subject: [PATCH] target identityLibrary --- .../IdentityCore.Library.csproj | 12 ++++ IdentityCore.Library/MyIdentityRole.cs | 26 ++++++++ IdentityCore.Library/MyIdentityUser.cs | 17 +++++ IdentityCore.Library/MyRoleStore.cs | 65 ++++++++++++++++++ IdentityCore.Library/MyUserStore.cs | 66 +++++++++++++++++++ WebApplication.sln | 31 +++++++++ .../Controllers/AccountController.cs | 16 ++--- .../Controllers/ManageController.cs | 12 ++-- WebApplication/Data/ApplicationDbContext.cs | 6 +- ...000000000_CreateIdentitySchema.Designer.cs | 8 +-- .../ApplicationDbContextModelSnapshot.cs | 8 +-- WebApplication/Models/ApplicationUser.cs | 12 ++-- WebApplication/Startup.cs | 16 +++-- WebApplication/Views/Account/Login.cshtml | 3 +- WebApplication/Views/Manage/_ManageNav.cshtml | 3 +- .../Views/Shared/_LoginPartial.cshtml | 6 +- WebApplication/WebApplication.csproj | 4 ++ WebApplication/appsettings.json | 2 +- 18 files changed, 270 insertions(+), 43 deletions(-) create mode 100644 IdentityCore.Library/IdentityCore.Library.csproj create mode 100644 IdentityCore.Library/MyIdentityRole.cs create mode 100644 IdentityCore.Library/MyIdentityUser.cs create mode 100644 IdentityCore.Library/MyRoleStore.cs create mode 100644 IdentityCore.Library/MyUserStore.cs create mode 100644 WebApplication.sln diff --git a/IdentityCore.Library/IdentityCore.Library.csproj b/IdentityCore.Library/IdentityCore.Library.csproj new file mode 100644 index 0000000..06e6be5 --- /dev/null +++ b/IdentityCore.Library/IdentityCore.Library.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp2.0 + + + + + + + + diff --git a/IdentityCore.Library/MyIdentityRole.cs b/IdentityCore.Library/MyIdentityRole.cs new file mode 100644 index 0000000..11149f0 --- /dev/null +++ b/IdentityCore.Library/MyIdentityRole.cs @@ -0,0 +1,26 @@ +namespace IdentityCore.Library +{ + using Microsoft.AspNetCore.Identity; + using System; + + public class MyIdentityRole : IdentityRole + { + public MyIdentityRole() + { + Id = Guid.NewGuid().ToString(); + } + + public MyIdentityRole(string name) : this() + { + Name = name; + } + + public MyIdentityRole(string name, string id) + { + Name = name; + Id = id; + } + + public string Description { get; internal set; } + } +} \ No newline at end of file diff --git a/IdentityCore.Library/MyIdentityUser.cs b/IdentityCore.Library/MyIdentityUser.cs new file mode 100644 index 0000000..630ad8a --- /dev/null +++ b/IdentityCore.Library/MyIdentityUser.cs @@ -0,0 +1,17 @@ +namespace IdentityCore.Library +{ + using Microsoft.AspNetCore.Identity; + using System; + + public class MyIdentityUser : IdentityUser + { + public DateTime? JoinDate { get; set; } + public string JobTitle { get; set; } + public string Contract { get; set; } + + public MyIdentityUser() + { + Id = Guid.NewGuid().ToString(); + } + } +} \ No newline at end of file diff --git a/IdentityCore.Library/MyRoleStore.cs b/IdentityCore.Library/MyRoleStore.cs new file mode 100644 index 0000000..d0f8896 --- /dev/null +++ b/IdentityCore.Library/MyRoleStore.cs @@ -0,0 +1,65 @@ +using Microsoft.AspNetCore.Identity; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace IdentityCore.Library +{ + public class MyRoleStore : IRoleStore where T : MyIdentityRole + { + public Task CreateAsync(T role, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(T role, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public void Dispose() + { + //throw new NotImplementedException(); + } + + public Task FindByIdAsync(string roleId, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task GetNormalizedRoleNameAsync(T role, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task GetRoleIdAsync(T role, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task GetRoleNameAsync(T role, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task SetNormalizedRoleNameAsync(T role, string normalizedName, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task SetRoleNameAsync(T role, string roleName, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task UpdateAsync(T role, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/IdentityCore.Library/MyUserStore.cs b/IdentityCore.Library/MyUserStore.cs new file mode 100644 index 0000000..a6e32d5 --- /dev/null +++ b/IdentityCore.Library/MyUserStore.cs @@ -0,0 +1,66 @@ +using Microsoft.AspNetCore.Identity; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace IdentityCore.Library +{ + public class MyUserStore : IUserStore where T : MyIdentityUser + { + public Task CreateAsync(T user, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(T user, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public void Dispose() + { + //throw new NotImplementedException(); + } + + public Task FindByIdAsync(string userId, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken) + { + //TODO : implement database logic to get the user by name + throw new NotImplementedException(); + } + + public Task GetNormalizedUserNameAsync(T user, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task GetUserIdAsync(T user, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task GetUserNameAsync(T user, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task SetNormalizedUserNameAsync(T user, string normalizedName, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task SetUserNameAsync(T user, string userName, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task UpdateAsync(T user, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/WebApplication.sln b/WebApplication.sln new file mode 100644 index 0000000..fe4ab25 --- /dev/null +++ b/WebApplication.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27130.2003 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication", "WebApplication\WebApplication.csproj", "{ACFBC569-D489-4CF0-90D6-BA973A1EF058}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdentityCore.Library", "IdentityCore.Library\IdentityCore.Library.csproj", "{EA25B28F-7C10-4815-9276-70E8DBEB11E9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {ACFBC569-D489-4CF0-90D6-BA973A1EF058}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ACFBC569-D489-4CF0-90D6-BA973A1EF058}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ACFBC569-D489-4CF0-90D6-BA973A1EF058}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ACFBC569-D489-4CF0-90D6-BA973A1EF058}.Release|Any CPU.Build.0 = Release|Any CPU + {EA25B28F-7C10-4815-9276-70E8DBEB11E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EA25B28F-7C10-4815-9276-70E8DBEB11E9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EA25B28F-7C10-4815-9276-70E8DBEB11E9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EA25B28F-7C10-4815-9276-70E8DBEB11E9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {530A232D-3D7A-4367-9825-7D79A21DCD51} + EndGlobalSection +EndGlobal diff --git a/WebApplication/Controllers/AccountController.cs b/WebApplication/Controllers/AccountController.cs index da4d8c8..bd10fcf 100644 --- a/WebApplication/Controllers/AccountController.cs +++ b/WebApplication/Controllers/AccountController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Authentication; +using IdentityCore.Library; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; @@ -6,7 +7,6 @@ using System; using System.Security.Claims; using System.Threading.Tasks; -using WebApplication.Models; using WebApplication.Models.AccountViewModels; using WebApplication.Services; @@ -16,14 +16,14 @@ namespace WebApplication.Controllers [Route("[controller]/[action]")] public class AccountController : Controller { - private readonly UserManager _userManager; - private readonly SignInManager _signInManager; + private readonly UserManager _userManager; + private readonly SignInManager _signInManager; private readonly IEmailSender _emailSender; private readonly ILogger _logger; public AccountController( - UserManager userManager, - SignInManager signInManager, + UserManager userManager, + SignInManager signInManager, IEmailSender emailSender, ILogger logger) { @@ -216,7 +216,7 @@ public async Task Register(RegisterViewModel model, string return ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { - var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; + var user = new MyIdentityUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { @@ -306,7 +306,7 @@ public async Task ExternalLoginConfirmation(ExternalLoginViewMode { throw new ApplicationException("Error loading external login information during confirmation."); } - var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; + var user = new MyIdentityUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { diff --git a/WebApplication/Controllers/ManageController.cs b/WebApplication/Controllers/ManageController.cs index d7355c5..1c6cbcb 100644 --- a/WebApplication/Controllers/ManageController.cs +++ b/WebApplication/Controllers/ManageController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Authentication; +using IdentityCore.Library; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; @@ -8,7 +9,6 @@ using System.Text; using System.Text.Encodings.Web; using System.Threading.Tasks; -using WebApplication.Models; using WebApplication.Models.ManageViewModels; using WebApplication.Services; @@ -18,8 +18,8 @@ namespace WebApplication.Controllers [Route("[controller]/[action]")] public class ManageController : Controller { - private readonly UserManager _userManager; - private readonly SignInManager _signInManager; + private readonly UserManager _userManager; + private readonly SignInManager _signInManager; private readonly IEmailSender _emailSender; private readonly ILogger _logger; private readonly UrlEncoder _urlEncoder; @@ -27,8 +27,8 @@ public class ManageController : Controller private const string AuthenicatorUriFormat = "otpauth://totp/{0}:{1}?secret={2}&issuer={0}&digits=6"; public ManageController( - UserManager userManager, - SignInManager signInManager, + UserManager userManager, + SignInManager signInManager, IEmailSender emailSender, ILogger logger, UrlEncoder urlEncoder) diff --git a/WebApplication/Data/ApplicationDbContext.cs b/WebApplication/Data/ApplicationDbContext.cs index 21c61a3..ffb3e81 100644 --- a/WebApplication/Data/ApplicationDbContext.cs +++ b/WebApplication/Data/ApplicationDbContext.cs @@ -1,10 +1,10 @@ -using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using IdentityCore.Library; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; -using WebApplication.Models; namespace WebApplication.Data { - public class ApplicationDbContext : IdentityDbContext + public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) diff --git a/WebApplication/Data/Migrations/00000000000000_CreateIdentitySchema.Designer.cs b/WebApplication/Data/Migrations/00000000000000_CreateIdentitySchema.Designer.cs index 6df5316..cdc13d6 100644 --- a/WebApplication/Data/Migrations/00000000000000_CreateIdentitySchema.Designer.cs +++ b/WebApplication/Data/Migrations/00000000000000_CreateIdentitySchema.Designer.cs @@ -126,7 +126,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.ToTable("AspNetUserTokens"); }); - modelBuilder.Entity("WebApplication.Models.ApplicationUser", b => + modelBuilder.Entity("WebApplication.Models.MyIdentityUser", b => { b.Property("Id"); @@ -185,7 +185,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => { - b.HasOne("WebApplication.Models.ApplicationUser") + b.HasOne("WebApplication.Models.MyIdentityUser") .WithMany("Claims") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade); @@ -193,7 +193,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => { - b.HasOne("WebApplication.Models.ApplicationUser") + b.HasOne("WebApplication.Models.MyIdentityUser") .WithMany("Logins") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade); @@ -206,7 +206,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasForeignKey("RoleId") .OnDelete(DeleteBehavior.Cascade); - b.HasOne("WebApplication.Models.ApplicationUser") + b.HasOne("WebApplication.Models.MyIdentityUser") .WithMany("Roles") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade); diff --git a/WebApplication/Data/Migrations/ApplicationDbContextModelSnapshot.cs b/WebApplication/Data/Migrations/ApplicationDbContextModelSnapshot.cs index 6dedd96..d9e6861 100644 --- a/WebApplication/Data/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/WebApplication/Data/Migrations/ApplicationDbContextModelSnapshot.cs @@ -121,7 +121,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("AspNetUserTokens"); }); - modelBuilder.Entity("WebApplication.Models.ApplicationUser", b => + modelBuilder.Entity("WebApplication.Models.MyIdentityUser", b => { b.Property("Id"); @@ -180,7 +180,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserClaim", b => { - b.HasOne("WebApplication.Models.ApplicationUser") + b.HasOne("WebApplication.Models.MyIdentityUser") .WithMany("Claims") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade); @@ -188,7 +188,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin", b => { - b.HasOne("WebApplication.Models.ApplicationUser") + b.HasOne("WebApplication.Models.MyIdentityUser") .WithMany("Logins") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade); @@ -201,7 +201,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasForeignKey("RoleId") .OnDelete(DeleteBehavior.Cascade); - b.HasOne("WebApplication.Models.ApplicationUser") + b.HasOne("WebApplication.Models.MyIdentityUser") .WithMany("Roles") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade); diff --git a/WebApplication/Models/ApplicationUser.cs b/WebApplication/Models/ApplicationUser.cs index 12e08b3..14881e3 100644 --- a/WebApplication/Models/ApplicationUser.cs +++ b/WebApplication/Models/ApplicationUser.cs @@ -1,9 +1,7 @@ -using Microsoft.AspNetCore.Identity; - -namespace WebApplication.Models +namespace WebApplication.Models { - // Add profile data for application users by adding properties to the ApplicationUser class - public class ApplicationUser : IdentityUser - { - } + // Add profile data for application users by adding properties to the MyIdentityUser class + //public class MyIdentityUser : IdentityUser + //{ + //} } \ No newline at end of file diff --git a/WebApplication/Startup.cs b/WebApplication/Startup.cs index 7f9b10d..b378461 100644 --- a/WebApplication/Startup.cs +++ b/WebApplication/Startup.cs @@ -1,11 +1,11 @@ -using Microsoft.AspNetCore.Builder; +using IdentityCore.Library; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using WebApplication.Data; -using WebApplication.Models; using WebApplication.Services; namespace WebApplication @@ -25,9 +25,15 @@ public void ConfigureServices(IServiceCollection services) services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); - services.AddIdentity() - .AddEntityFrameworkStores() - .AddDefaultTokenProviders(); + services.AddIdentity>( + config => + { + config.SignIn.RequireConfirmedEmail = true; + }) + .AddDefaultTokenProviders(); + + services.AddTransient, MyUserStore>(); + services.AddTransient>, MyRoleStore>>(); // Add application services. services.AddTransient(); diff --git a/WebApplication/Views/Account/Login.cshtml b/WebApplication/Views/Account/Login.cshtml index 291af5c..8114201 100644 --- a/WebApplication/Views/Account/Login.cshtml +++ b/WebApplication/Views/Account/Login.cshtml @@ -2,8 +2,9 @@ @using System.Linq @using Microsoft.AspNetCore.Http @using Microsoft.AspNetCore.Http.Authentication +@using IdentityCore.Library; @model LoginViewModel -@inject SignInManager SignInManager +@inject SignInManager SignInManager @{ ViewData["Title"] = "Log in"; diff --git a/WebApplication/Views/Manage/_ManageNav.cshtml b/WebApplication/Views/Manage/_ManageNav.cshtml index ccda819..e7af2df 100644 --- a/WebApplication/Views/Manage/_ManageNav.cshtml +++ b/WebApplication/Views/Manage/_ManageNav.cshtml @@ -1,5 +1,6 @@ @using WebApplication.Views.Manage -@inject SignInManager SignInManager +@using IdentityCore.Library; +@inject SignInManager SignInManager @{ var hasExternalLogins = (await SignInManager.GetExternalAuthenticationSchemesAsync()).Any(); } diff --git a/WebApplication/Views/Shared/_LoginPartial.cshtml b/WebApplication/Views/Shared/_LoginPartial.cshtml index b9519c3..ce76862 100644 --- a/WebApplication/Views/Shared/_LoginPartial.cshtml +++ b/WebApplication/Views/Shared/_LoginPartial.cshtml @@ -1,8 +1,8 @@ @using Microsoft.AspNetCore.Identity @using WebApplication.Models - -@inject SignInManager SignInManager -@inject UserManager UserManager +@using IdentityCore.Library; +@inject SignInManager SignInManager +@inject UserManager UserManager @if (SignInManager.IsSignedIn(User)) { diff --git a/WebApplication/WebApplication.csproj b/WebApplication/WebApplication.csproj index 350684d..c7a5cdb 100644 --- a/WebApplication/WebApplication.csproj +++ b/WebApplication/WebApplication.csproj @@ -18,4 +18,8 @@ + + + + diff --git a/WebApplication/appsettings.json b/WebApplication/appsettings.json index cee0729..5b0e66b 100644 --- a/WebApplication/appsettings.json +++ b/WebApplication/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplication-8005CEA4-199F-4399-93EA-8AEFF965FED5;Trusted_Connection=True;MultipleActiveResultSets=true" + "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=How-to-Customize-Asp.NET-Identity-Core-with-External-Database-Storage;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Logging": { "IncludeScopes": false,