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
+ public class ApplicationUserManager : UserManager < ApplicationUser >
19
+ {
20
+ public ApplicationUserManager ( IUserStore < ApplicationUser > store )
21
+ : base ( store )
22
+ {
23
+ }
24
+
25
+ public static ApplicationUserManager Create ( IdentityFactoryOptions < ApplicationUserManager > options ,
26
+ IOwinContext context )
27
+ {
28
+ var manager = new ApplicationUserManager ( new UserStore < ApplicationUser > ( context . Get < ApplicationDbContext > ( ) ) ) ;
29
+ // Configure validation logic for usernames
30
+ manager . UserValidator = new UserValidator < ApplicationUser > ( manager )
31
+ {
32
+ AllowOnlyAlphanumericUserNames = false ,
33
+ RequireUniqueEmail = true
34
+ } ;
35
+ // Configure validation logic for passwords
36
+ manager . PasswordValidator = new PasswordValidator
37
+ {
38
+ RequiredLength = 6 ,
39
+ RequireNonLetterOrDigit = true ,
40
+ RequireDigit = true ,
41
+ RequireLowercase = true ,
42
+ RequireUppercase = true ,
43
+ } ;
44
+ // Configure user lockout defaults
45
+ manager . UserLockoutEnabledByDefault = true ;
46
+ manager . DefaultAccountLockoutTimeSpan = TimeSpan . FromMinutes ( 5 ) ;
47
+ manager . MaxFailedAccessAttemptsBeforeLockout = 5 ;
48
+ // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
49
+ // You can write your own provider and plug in here.
50
+ manager . RegisterTwoFactorProvider ( "PhoneCode" , new PhoneNumberTokenProvider < ApplicationUser >
51
+ {
52
+ MessageFormat = "Your security code is: {0}"
53
+ } ) ;
54
+ manager . RegisterTwoFactorProvider ( "EmailCode" , new EmailTokenProvider < ApplicationUser >
55
+ {
56
+ Subject = "SecurityCode" ,
57
+ BodyFormat = "Your security code is {0}"
58
+ } ) ;
59
+ manager . EmailService = new EmailService ( ) ;
60
+ manager . SmsService = new SmsService ( ) ;
61
+ var dataProtectionProvider = options . DataProtectionProvider ;
62
+ if ( dataProtectionProvider != null )
63
+ {
64
+ manager . UserTokenProvider =
65
+ new DataProtectorTokenProvider < ApplicationUser > ( dataProtectionProvider . Create ( "ASP.NET Identity" ) ) ;
66
+ }
67
+ return manager ;
68
+ }
69
+ }
70
+
71
+ // Configure the RoleManager used in the application. RoleManager is defined in the ASP.NET Identity core assembly
72
+ public class ApplicationRoleManager : RoleManager < IdentityRole >
73
+ {
74
+ public ApplicationRoleManager ( IRoleStore < IdentityRole , string > roleStore )
75
+ : base ( roleStore )
76
+ {
77
+ }
78
+
79
+ public static ApplicationRoleManager Create ( IdentityFactoryOptions < ApplicationRoleManager > options , IOwinContext context )
80
+ {
81
+ return new ApplicationRoleManager ( new RoleStore < IdentityRole > ( context . Get < ApplicationDbContext > ( ) ) ) ;
82
+ }
83
+ }
84
+
85
+ public class EmailService : IIdentityMessageService
86
+ {
87
+ public Task SendAsync ( IdentityMessage message )
88
+ {
89
+ // Plug in your email service here to send an email.
90
+ return Task . FromResult ( 0 ) ;
91
+ }
92
+ }
93
+
94
+ public class SmsService : IIdentityMessageService
95
+ {
96
+ public Task SendAsync ( IdentityMessage message )
97
+ {
98
+ // Plug in your sms service here to send a text message.
99
+ return Task . FromResult ( 0 ) ;
100
+ }
101
+ }
102
+
103
+ // This is useful if you do not want to tear down the database each time you run the application.
104
+ // public class ApplicationDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
105
+ // This example shows you how to create a new database if the Model changes
106
+ public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges < ApplicationDbContext >
107
+ {
108
+ protected override void Seed ( ApplicationDbContext context ) {
109
+ InitializeIdentityForEF ( context ) ;
110
+ base . Seed ( context ) ;
111
+ }
112
+
113
+ //Create [email protected] with password=Admin@123456 in the Admin role
114
+ public static void InitializeIdentityForEF ( ApplicationDbContext db ) {
115
+ var userManager = HttpContext . Current . GetOwinContext ( ) . GetUserManager < ApplicationUserManager > ( ) ;
116
+ var roleManager = HttpContext . Current . GetOwinContext ( ) . Get < ApplicationRoleManager > ( ) ;
117
+ const string name = "[email protected] " ;
118
+ const string password = "Admin@123456" ;
119
+ const string roleName = "Admin" ;
120
+
121
+ //Create Role Admin if it does not exist
122
+ var role = roleManager . FindByName ( roleName ) ;
123
+ if ( role == null ) {
124
+ role = new IdentityRole ( roleName ) ;
125
+ var roleresult = roleManager . Create ( role ) ;
126
+ }
127
+
128
+ var user = userManager . FindByName ( name ) ;
129
+ if ( user == null ) {
130
+ user = new ApplicationUser { UserName = name , Email = name } ;
131
+ var result = userManager . Create ( user , password ) ;
132
+ result = userManager . SetLockoutEnabled ( user . Id , false ) ;
133
+ }
134
+
135
+ // Add user admin to Role Admin if not already added
136
+ var rolesForUser = userManager . GetRoles ( user . Id ) ;
137
+ if ( ! rolesForUser . Contains ( role . Name ) ) {
138
+ var result = userManager . AddToRole ( user . Id , role . Name ) ;
139
+ }
140
+ }
141
+ }
142
+
143
+ public class ApplicationSignInManager : SignInManager < ApplicationUser , string >
144
+ {
145
+ public ApplicationSignInManager ( ApplicationUserManager userManager , IAuthenticationManager authenticationManager ) :
146
+ base ( userManager , authenticationManager ) { }
147
+
148
+ public override Task < ClaimsIdentity > CreateUserIdentityAsync ( ApplicationUser user )
149
+ {
150
+ return user . GenerateUserIdentityAsync ( ( ApplicationUserManager ) UserManager ) ;
151
+ }
152
+
153
+ public static ApplicationSignInManager Create ( IdentityFactoryOptions < ApplicationSignInManager > options , IOwinContext context )
154
+ {
155
+ return new ApplicationSignInManager ( context . GetUserManager < ApplicationUserManager > ( ) , context . Authentication ) ;
156
+ }
157
+ }
158
+ }
0 commit comments