Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.41 KB | None | 0 0
  1. using System;
  2. using System.Security.Claims;
  3. using System.Threading.Tasks;
  4. using Domain;
  5. using Domain.Entities;
  6. using Microsoft.AspNet.Identity;
  7. using Microsoft.AspNet.Identity.EntityFramework;
  8. using Microsoft.AspNet.Identity.Owin;
  9. using Microsoft.Owin;
  10. using Microsoft.Owin.Security;
  11. using Microsoft.Owin.Security.DataProtection;
  12.  
  13. namespace CKBase
  14. {
  15.    public class EmailService : IIdentityMessageService
  16.    {
  17.       #region Methods
  18.  
  19.       public Task SendAsync(IdentityMessage message)
  20.       {
  21.          // Plug in your email service here to send an email.
  22.          return Task.FromResult(0);
  23.       }
  24.  
  25.       #endregion
  26.    }
  27.  
  28.    public class SmsService : IIdentityMessageService
  29.    {
  30.       #region Methods
  31.  
  32.       public Task SendAsync(IdentityMessage message)
  33.       {
  34.          // Plug in your SMS service here to send a text message.
  35.          return Task.FromResult(0);
  36.       }
  37.  
  38.       #endregion
  39.    }
  40.  
  41.    // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
  42.    public class ApplicationUserManager : UserManager<User>
  43.    {
  44.       #region Constructor(s), Destructor, and Dispose
  45.  
  46.       public ApplicationUserManager(IUserStore<User> store)
  47.          : base(store)
  48.       {}
  49.  
  50.       #endregion
  51.       #region Methods
  52.  
  53.       public static ApplicationUserManager Create(
  54.          IdentityFactoryOptions<ApplicationUserManager> options,
  55.          IOwinContext context)
  56.       {
  57.          var manager = new ApplicationUserManager(new UserStore<User>(context.Get<YourDbNameHere>()));
  58.          // Configure validation logic for usernames
  59.          manager.UserValidator = new UserValidator<User>(manager)
  60.          {
  61.             AllowOnlyAlphanumericUserNames = false,
  62.             RequireUniqueEmail = true
  63.          };
  64.  
  65.          // Configure validation logic for passwords
  66.          manager.PasswordValidator = new PasswordValidator
  67.          {
  68.             RequiredLength = 6,
  69.             RequireNonLetterOrDigit = true,
  70.             RequireDigit = true,
  71.             RequireLowercase = true,
  72.             RequireUppercase = true,
  73.          };
  74.  
  75.          // Configure user lockout defaults
  76.          manager.UserLockoutEnabledByDefault = true;
  77.          manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
  78.          manager.MaxFailedAccessAttemptsBeforeLockout = 5;
  79.  
  80.          // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
  81.          // You can write your own provider and plug it in here.
  82.          manager.RegisterTwoFactorProvider("Phone Code",
  83.             new PhoneNumberTokenProvider<User>
  84.             {
  85.                MessageFormat = "Your security code is {0}"
  86.             });
  87.          manager.RegisterTwoFactorProvider("Email Code",
  88.             new EmailTokenProvider<User>
  89.             {
  90.                Subject = "Security Code",
  91.                BodyFormat = "Your security code is {0}"
  92.             });
  93.          manager.EmailService = new EmailService();
  94.          manager.SmsService = new SmsService();
  95.          IDataProtectionProvider dataProtectionProvider = options.DataProtectionProvider;
  96.          if(dataProtectionProvider != null)
  97.          {
  98.             manager.UserTokenProvider =
  99.                new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"));
  100.          }
  101.          return manager;
  102.       }
  103.  
  104.       #endregion
  105.    }
  106.  
  107.    // Configure the application sign-in manager which is used in this application.
  108.    public class ApplicationSignInManager : SignInManager<User, string>
  109.    {
  110.       #region Constructor(s), Destructor, and Dispose
  111.  
  112.       public ApplicationSignInManager(ApplicationUserManager userManager,
  113.          IAuthenticationManager authenticationManager)
  114.          : base(userManager, authenticationManager)
  115.       {}
  116.  
  117.       #endregion
  118.       #region Methods
  119.  
  120.       public static ApplicationSignInManager Create(
  121.          IdentityFactoryOptions<ApplicationSignInManager> options,
  122.          IOwinContext context)
  123.       {
  124.          return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(),
  125.             context.Authentication);
  126.       }
  127.  
  128.       public override Task<ClaimsIdentity> CreateUserIdentityAsync(User user)
  129.       {
  130.          return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
  131.       }
  132.  
  133.       #endregion
  134.    }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement