Advertisement
Guest User

their identityconfig(seed inside)

a guest
May 4th, 2017
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.45 KB | None | 0 0
  1. using System.Linq;
  2. using System.Security.Claims;
  3. using Microsoft.AspNet.Identity;
  4. using Microsoft.AspNet.Identity.EntityFramework;
  5. using Microsoft.AspNet.Identity.Owin;
  6. using Microsoft.Owin;
  7. using Microsoft.Owin.Security;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data.Entity;
  11. using System.Threading.Tasks;
  12. using System.Web;
  13.  
  14. namespace IdentitySample.Models
  15. {
  16.     // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
  17.  
  18.     // *** PASS IN TYPE ARGUMENT TO BASE CLASS:
  19.     public class ApplicationUserManager : UserManager<ApplicationUser, int>
  20.     {
  21.         // *** ADD INT TYPE ARGUMENT TO CONSTRUCTOR CALL:
  22.         public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
  23.             : base(store)
  24.         {
  25.         }
  26.  
  27.         public static ApplicationUserManager Create(
  28.             IdentityFactoryOptions<ApplicationUserManager> options,
  29.             IOwinContext context)
  30.         {
  31.             // *** PASS CUSTOM APPLICATION USER STORE AS CONSTRUCTOR ARGUMENT:
  32.             var manager = new ApplicationUserManager(
  33.                 new ApplicationUserStore(context.Get<ApplicationDbContext>()));
  34.  
  35.             // Configure validation logic for usernames
  36.  
  37.             // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
  38.             manager.UserValidator = new UserValidator<ApplicationUser, int>(manager)
  39.             {
  40.                 AllowOnlyAlphanumericUserNames = false,
  41.                 RequireUniqueEmail = true
  42.             };
  43.  
  44.             // Configure validation logic for passwords
  45.             manager.PasswordValidator = new PasswordValidator
  46.             {
  47.                 RequiredLength = 6,
  48.                 RequireNonLetterOrDigit = true,
  49.                 RequireDigit = true,
  50.                 RequireLowercase = true,
  51.                 RequireUppercase = true,
  52.             };
  53.  
  54.             // Configure user lockout defaults
  55.             manager.UserLockoutEnabledByDefault = true;
  56.             manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
  57.             manager.MaxFailedAccessAttemptsBeforeLockout = 5;
  58.  
  59.             // Register two factor authentication providers.
  60.             // This application uses Phone and Emails as a step of receiving a
  61.             // code for verifying the user You can write your own provider and plug in here.
  62.  
  63.             // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
  64.             manager.RegisterTwoFactorProvider("PhoneCode",
  65.                 new PhoneNumberTokenProvider<ApplicationUser, int>
  66.                 {
  67.                     MessageFormat = "Your security code is: {0}"
  68.                 });
  69.  
  70.             // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
  71.             manager.RegisterTwoFactorProvider("EmailCode",
  72.                 new EmailTokenProvider<ApplicationUser, int>
  73.                 {
  74.                     Subject = "SecurityCode",
  75.                     BodyFormat = "Your security code is {0}"
  76.                 });
  77.  
  78.             manager.EmailService = new EmailService();
  79.             manager.SmsService = new SmsService();
  80.             var dataProtectionProvider = options.DataProtectionProvider;
  81.             if (dataProtectionProvider != null)
  82.             {
  83.                 // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
  84.                 manager.UserTokenProvider =
  85.                     new DataProtectorTokenProvider<ApplicationUser, int>(
  86.                         dataProtectionProvider.Create("ASP.NET Identity"));
  87.             }
  88.             return manager;
  89.         }
  90.     }
  91.  
  92.  
  93.     // PASS CUSTOM APPLICATION ROLE AND INT AS TYPE ARGUMENTS TO BASE:
  94.     public class ApplicationRoleManager : RoleManager<ApplicationRole, int>
  95.     {
  96.         // PASS CUSTOM APPLICATION ROLE AND INT AS TYPE ARGUMENTS TO CONSTRUCTOR:
  97.         public ApplicationRoleManager(IRoleStore<ApplicationRole, int> roleStore)
  98.             : base(roleStore)
  99.         {
  100.         }
  101.  
  102.         // PASS CUSTOM APPLICATION ROLE AS TYPE ARGUMENT:
  103.         public static ApplicationRoleManager Create(
  104.             IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
  105.         {
  106.             return new ApplicationRoleManager(
  107.                 new ApplicationRoleStore(context.Get<ApplicationDbContext>()));
  108.         }
  109.     }
  110.  
  111.  
  112.     public class EmailService : IIdentityMessageService
  113.     {
  114.         public Task SendAsync(IdentityMessage message)
  115.         {
  116.             // Plug in your email service here to send an email.
  117.             return Task.FromResult(0);
  118.         }
  119.     }
  120.  
  121.  
  122.     public class SmsService : IIdentityMessageService
  123.     {
  124.         public Task SendAsync(IdentityMessage message)
  125.         {
  126.             // Plug in your sms service here to send a text message.
  127.             return Task.FromResult(0);
  128.         }
  129.     }
  130.  
  131.     // This is useful if you do not want to tear down the database each time you run the application.
  132.     // public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
  133.     // This example shows you how to create a new database if the Model changes
  134.     public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
  135.     {
  136.         protected override void Seed(ApplicationDbContext context) {
  137.             InitializeIdentityForEF(context);
  138.             base.Seed(context);
  139.         }
  140.  
  141.         //Create User=Admin@Admin.com with password=Admin@123456 in the Admin role        
  142.         public static void InitializeIdentityForEF(ApplicationDbContext db) {
  143.             var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
  144.             var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
  145.             const string name = "admin@example.com";
  146.             const string password = "Admin@123456";
  147.             const string roleName = "Admin";
  148.  
  149.             //Create Role Admin if it does not exist
  150.             var role = roleManager.FindByName(roleName);
  151.             if (role == null) {
  152.                 role = new ApplicationRole(roleName);
  153.                 var roleresult = roleManager.Create(role);
  154.             }
  155.  
  156.             var user = userManager.FindByName(name);
  157.             if (user == null) {
  158.                 user = new ApplicationUser { UserName = name, Email = name };
  159.                 var result = userManager.Create(user, password);
  160.                 result = userManager.SetLockoutEnabled(user.Id, false);
  161.             }
  162.  
  163.             // Add user admin to Role Admin if not already added
  164.             var rolesForUser = userManager.GetRoles(user.Id);
  165.             if (!rolesForUser.Contains(role.Name)) {
  166.                 var result = userManager.AddToRole(user.Id, role.Name);
  167.             }
  168.         }
  169.     }
  170.  
  171.  
  172.     public class ApplicationSignInManager : SignInManager<ApplicationUser, int>
  173.     {
  174.         public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) :
  175.             base(userManager, authenticationManager) { }
  176.  
  177.         public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
  178.         {
  179.             return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
  180.         }
  181.  
  182.         public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
  183.         {
  184.             return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement