Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- using System.Security.Claims;
- using Microsoft.AspNet.Identity;
- using Microsoft.AspNet.Identity.EntityFramework;
- using Microsoft.AspNet.Identity.Owin;
- using Microsoft.Owin;
- using Microsoft.Owin.Security;
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Threading.Tasks;
- using System.Web;
- namespace IdentitySample.Models
- {
- // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
- // *** PASS IN TYPE ARGUMENT TO BASE CLASS:
- public class ApplicationUserManager : UserManager<ApplicationUser, int>
- {
- // *** ADD INT TYPE ARGUMENT TO CONSTRUCTOR CALL:
- public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
- : base(store)
- {
- }
- public static ApplicationUserManager Create(
- IdentityFactoryOptions<ApplicationUserManager> options,
- IOwinContext context)
- {
- // *** PASS CUSTOM APPLICATION USER STORE AS CONSTRUCTOR ARGUMENT:
- var manager = new ApplicationUserManager(
- new ApplicationUserStore(context.Get<ApplicationDbContext>()));
- // Configure validation logic for usernames
- // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
- manager.UserValidator = new UserValidator<ApplicationUser, int>(manager)
- {
- AllowOnlyAlphanumericUserNames = false,
- RequireUniqueEmail = true
- };
- // Configure validation logic for passwords
- manager.PasswordValidator = new PasswordValidator
- {
- RequiredLength = 6,
- RequireNonLetterOrDigit = true,
- RequireDigit = true,
- RequireLowercase = true,
- RequireUppercase = true,
- };
- // Configure user lockout defaults
- manager.UserLockoutEnabledByDefault = true;
- manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
- manager.MaxFailedAccessAttemptsBeforeLockout = 5;
- // Register two factor authentication providers.
- // This application uses Phone and Emails as a step of receiving a
- // code for verifying the user You can write your own provider and plug in here.
- // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
- manager.RegisterTwoFactorProvider("PhoneCode",
- new PhoneNumberTokenProvider<ApplicationUser, int>
- {
- MessageFormat = "Your security code is: {0}"
- });
- // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
- manager.RegisterTwoFactorProvider("EmailCode",
- new EmailTokenProvider<ApplicationUser, int>
- {
- Subject = "SecurityCode",
- BodyFormat = "Your security code is {0}"
- });
- manager.EmailService = new EmailService();
- manager.SmsService = new SmsService();
- var dataProtectionProvider = options.DataProtectionProvider;
- if (dataProtectionProvider != null)
- {
- // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
- manager.UserTokenProvider =
- new DataProtectorTokenProvider<ApplicationUser, int>(
- dataProtectionProvider.Create("ASP.NET Identity"));
- }
- return manager;
- }
- }
- // PASS CUSTOM APPLICATION ROLE AND INT AS TYPE ARGUMENTS TO BASE:
- public class ApplicationRoleManager : RoleManager<ApplicationRole, int>
- {
- // PASS CUSTOM APPLICATION ROLE AND INT AS TYPE ARGUMENTS TO CONSTRUCTOR:
- public ApplicationRoleManager(IRoleStore<ApplicationRole, int> roleStore)
- : base(roleStore)
- {
- }
- // PASS CUSTOM APPLICATION ROLE AS TYPE ARGUMENT:
- public static ApplicationRoleManager Create(
- IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
- {
- return new ApplicationRoleManager(
- new ApplicationRoleStore(context.Get<ApplicationDbContext>()));
- }
- }
- public class EmailService : IIdentityMessageService
- {
- public Task SendAsync(IdentityMessage message)
- {
- // Plug in your email service here to send an email.
- return Task.FromResult(0);
- }
- }
- public class SmsService : IIdentityMessageService
- {
- public Task SendAsync(IdentityMessage message)
- {
- // Plug in your sms service here to send a text message.
- return Task.FromResult(0);
- }
- }
- // This is useful if you do not want to tear down the database each time you run the application.
- // public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
- // This example shows you how to create a new database if the Model changes
- public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
- {
- protected override void Seed(ApplicationDbContext context) {
- InitializeIdentityForEF(context);
- base.Seed(context);
- }
- //Create User=Admin@Admin.com with password=Admin@123456 in the Admin role
- public static void InitializeIdentityForEF(ApplicationDbContext db) {
- var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
- var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
- const string name = "admin@example.com";
- const string password = "Admin@123456";
- const string roleName = "Admin";
- //Create Role Admin if it does not exist
- var role = roleManager.FindByName(roleName);
- if (role == null) {
- role = new ApplicationRole(roleName);
- var roleresult = roleManager.Create(role);
- }
- var user = userManager.FindByName(name);
- if (user == null) {
- user = new ApplicationUser { UserName = name, Email = name };
- var result = userManager.Create(user, password);
- result = userManager.SetLockoutEnabled(user.Id, false);
- }
- // Add user admin to Role Admin if not already added
- var rolesForUser = userManager.GetRoles(user.Id);
- if (!rolesForUser.Contains(role.Name)) {
- var result = userManager.AddToRole(user.Id, role.Name);
- }
- }
- }
- public class ApplicationSignInManager : SignInManager<ApplicationUser, int>
- {
- public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) :
- base(userManager, authenticationManager) { }
- public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
- {
- return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
- }
- public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
- {
- return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement