-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
2,021 additions
and
506 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
using System.Web; | ||
using System.Web.Mvc; | ||
using System.Web.Mvc; | ||
|
||
namespace SAT.MVC.UI | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.Entity; | ||
using System.Linq; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
using Microsoft.AspNet.Identity; | ||
using Microsoft.AspNet.Identity.EntityFramework; | ||
using Microsoft.AspNet.Identity.Owin; | ||
using Microsoft.Owin; | ||
using Microsoft.Owin.Security; | ||
using SAT.MVC.UI.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.Entity; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
|
||
namespace SAT.MVC.UI | ||
namespace SAT.MVC.UI.Models | ||
{ | ||
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); | ||
} | ||
} | ||
|
||
// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application. | ||
|
||
public class ApplicationUserManager : UserManager<ApplicationUser> | ||
{ | ||
public ApplicationUserManager(IUserStore<ApplicationUser> store) | ||
: base(store) | ||
{ | ||
} | ||
|
||
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) | ||
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, | ||
IOwinContext context) | ||
{ | ||
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); | ||
// Configure validation logic for usernames | ||
|
@@ -49,7 +32,6 @@ public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUs | |
AllowOnlyAlphanumericUserNames = false, | ||
RequireUniqueEmail = true | ||
}; | ||
|
||
// Configure validation logic for passwords | ||
manager.PasswordValidator = new PasswordValidator | ||
{ | ||
|
@@ -59,42 +41,109 @@ public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUs | |
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 it in here. | ||
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser> | ||
// You can write your own provider and plug in here. | ||
manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider<ApplicationUser> | ||
{ | ||
MessageFormat = "Your security code is {0}" | ||
MessageFormat = "Your security code is: {0}" | ||
}); | ||
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser> | ||
manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<ApplicationUser> | ||
{ | ||
Subject = "Security Code", | ||
Subject = "SecurityCode", | ||
BodyFormat = "Your security code is {0}" | ||
}); | ||
manager.EmailService = new EmailService(); | ||
manager.SmsService = new SmsService(); | ||
var dataProtectionProvider = options.DataProtectionProvider; | ||
if (dataProtectionProvider != null) | ||
{ | ||
manager.UserTokenProvider = | ||
manager.UserTokenProvider = | ||
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity")); | ||
} | ||
return manager; | ||
} | ||
} | ||
|
||
// Configure the application sign-in manager which is used in this application. | ||
public class ApplicationSignInManager : SignInManager<ApplicationUser, string> | ||
// Configure the RoleManager used in the application. RoleManager is defined in the ASP.NET Identity core assembly | ||
public class ApplicationRoleManager : RoleManager<IdentityRole> | ||
{ | ||
public ApplicationRoleManager(IRoleStore<IdentityRole,string> roleStore) | ||
: base(roleStore) | ||
{ | ||
} | ||
|
||
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context) | ||
{ | ||
return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>())); | ||
} | ||
} | ||
|
||
public class EmailService : IIdentityMessageService | ||
{ | ||
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) | ||
: base(userManager, authenticationManager) | ||
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 : DropCreateDatabaseIfModelChanges<ApplicationDbContext> | ||
{ | ||
protected override void Seed(ApplicationDbContext context) { | ||
InitializeIdentityForEF(context); | ||
base.Seed(context); | ||
} | ||
|
||
//Create [email protected] 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 = "[email protected]"; | ||
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 IdentityRole(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, string> | ||
{ | ||
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) : | ||
base(userManager, authenticationManager) { } | ||
|
||
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user) | ||
{ | ||
|
@@ -106,4 +155,4 @@ public static ApplicationSignInManager Create(IdentityFactoryOptions<Application | |
return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.