faizinfy

IdentityConfig.vb

Dec 12th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.25 KB | None | 0 0
  1. Imports System.Threading.Tasks
  2. Imports System.Security.Claims
  3. Imports Microsoft.AspNet.Identity
  4. Imports Microsoft.AspNet.Identity.EntityFramework
  5. Imports Microsoft.AspNet.Identity.Owin
  6. Imports Microsoft.Owin
  7. Imports Microsoft.Owin.Security
  8.  
  9. Public Class EmailService
  10.     Implements IIdentityMessageService
  11.  
  12.     Public Function SendAsync(message As IdentityMessage) As Task Implements IIdentityMessageService.SendAsync
  13.         ' Plug in your email service here to send an email.
  14.         Return Task.FromResult(0)
  15.     End Function
  16. End Class
  17.  
  18. Public Class SmsService
  19.     Implements IIdentityMessageService
  20.  
  21.     Public Function SendAsync(message As IdentityMessage) As Task Implements IIdentityMessageService.SendAsync
  22.         ' Plug in your SMS service here to send a text message.
  23.         Return Task.FromResult(0)
  24.     End Function
  25. End Class
  26.  
  27. ' Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
  28. Public Class ApplicationUserManager
  29.     Inherits UserManager(Of ApplicationUser)
  30.  
  31.     Public Sub New(store As IUserStore(Of ApplicationUser))
  32.         MyBase.New(store)
  33.     End Sub
  34.  
  35.     Public Shared Function Create(options As IdentityFactoryOptions(Of ApplicationUserManager), context As IOwinContext) As ApplicationUserManager
  36.         Dim manager = New ApplicationUserManager(New UserStore(Of ApplicationUser)(context.Get(Of ApplicationDbContext)()))
  37.  
  38.         ' Configure validation logic for usernames
  39.         manager.UserValidator = New UserValidator(Of ApplicationUser)(manager) With {
  40.             .AllowOnlyAlphanumericUserNames = False,
  41.             .RequireUniqueEmail = True
  42.         }
  43.  
  44.         ' Configure validation logic for passwords
  45.         manager.PasswordValidator = New PasswordValidator With {
  46.             .RequiredLength = 6,
  47.             .RequireNonLetterOrDigit = True,
  48.             .RequireDigit = True,
  49.             .RequireLowercase = True,
  50.             .RequireUppercase = True
  51.         }
  52.  
  53.         ' Configure user lockout defaults
  54.         manager.UserLockoutEnabledByDefault = True
  55.         manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5)
  56.         manager.MaxFailedAccessAttemptsBeforeLockout = 5
  57.  
  58.         ' Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
  59.         ' You can write your own provider and plug it in here.
  60.         manager.RegisterTwoFactorProvider("Phone Code", New PhoneNumberTokenProvider(Of ApplicationUser) With {
  61.                                           .MessageFormat = "Your security code is {0}"
  62.                                       })
  63.         manager.RegisterTwoFactorProvider("Email Code", New EmailTokenProvider(Of ApplicationUser) With {
  64.                                           .Subject = "Security Code",
  65.                                           .BodyFormat = "Your security code is {0}"
  66.                                           })
  67.         manager.EmailService = New EmailService()
  68.         manager.SmsService = New SmsService()
  69.         Dim dataProtectionProvider = options.DataProtectionProvider
  70.         If (dataProtectionProvider IsNot Nothing) Then
  71.             manager.UserTokenProvider = New DataProtectorTokenProvider(Of ApplicationUser)(dataProtectionProvider.Create("ASP.NET Identity"))
  72.         End If
  73.  
  74.         Return manager
  75.     End Function
  76.  
  77. End Class
  78.  
  79. ' Configure the application sign-in manager which is used in this application.
  80. Public Class ApplicationSignInManager
  81.     Inherits SignInManager(Of ApplicationUser, String)
  82.     Public Sub New(userManager As ApplicationUserManager, authenticationManager As IAuthenticationManager)
  83.         MyBase.New(userManager, authenticationManager)
  84.     End Sub
  85.  
  86.     Public Overrides Function CreateUserIdentityAsync(user As ApplicationUser) As Task(Of ClaimsIdentity)
  87.         Return user.GenerateUserIdentityAsync(DirectCast(UserManager, ApplicationUserManager))
  88.     End Function
  89.  
  90.     Public Shared Function Create(options As IdentityFactoryOptions(Of ApplicationSignInManager), context As IOwinContext) As ApplicationSignInManager
  91.         Return New ApplicationSignInManager(context.GetUserManager(Of ApplicationUserManager)(), context.Authentication)
  92.     End Function
  93. End Class
Advertisement
Add Comment
Please, Sign In to add comment