Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. using System.Threading.Tasks;
  2. using Microsoft.AspNet.Identity;
  3. using Microsoft.AspNet.Identity.EntityFramework;
  4. using Microsoft.AspNet.Identity.Owin;
  5. using Microsoft.Owin;
  6. using ProjekatRad.Models;
  7. using SendGrid;
  8. using System.Net;
  9. using System;
  10. using System.Diagnostics;
  11. using System.Configuration;
  12.  
  13. namespace ProjekatRad
  14. {
  15. // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
  16.  
  17. public class ApplicationUserManager : UserManager<ApplicationUser>
  18. {
  19. public ApplicationUserManager(IUserStore<ApplicationUser> store)
  20. : base(store)
  21. {
  22. }
  23.  
  24. public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
  25. {
  26. var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
  27. // Configure validation logic for usernames
  28. manager.UserValidator = new UserValidator<ApplicationUser>(manager)
  29. {
  30. AllowOnlyAlphanumericUserNames = false,
  31. RequireUniqueEmail = true
  32. };
  33. // Configure validation logic for passwords
  34. manager.PasswordValidator = new PasswordValidator
  35. {
  36. RequiredLength = 6,
  37. RequireNonLetterOrDigit = false,
  38. RequireDigit = true,
  39. RequireLowercase = true,
  40. RequireUppercase = true,
  41. };
  42. // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
  43. // You can write your own provider and plug in here.
  44. manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider<ApplicationUser>
  45. {
  46. MessageFormat = "Your security code is: {0}"
  47. });
  48. manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<ApplicationUser>
  49. {
  50. Subject = "Security Code",
  51. BodyFormat = "Your security code is: {0}"
  52. });
  53. manager.EmailService = new EmailService();
  54. manager.SmsService = new SmsService();
  55. var dataProtectionProvider = options.DataProtectionProvider;
  56. if (dataProtectionProvider != null)
  57. {
  58. manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
  59. }
  60. return manager;
  61. }
  62. }
  63.  
  64. public class EmailService : IIdentityMessageService
  65. {
  66. public async Task SendAsync(IdentityMessage message)
  67. {
  68. await configSendGridasync(message);
  69. }
  70.  
  71. // Use NuGet to install SendGrid (Basic C# client lib)
  72. private async Task configSendGridasync(IdentityMessage message)
  73. {
  74. var myMessage = new SendGridMessage();
  75. myMessage.AddTo(message.Destination);
  76. myMessage.From = new System.Net.Mail.MailAddress(
  77. "IEPProjekat@iep.com", "IEP Projekat");
  78. myMessage.Subject = message.Subject;
  79. myMessage.Text = message.Body;
  80. myMessage.Html = message.Body;
  81.  
  82. var credentials = new NetworkCredential(
  83. ConfigurationManager.AppSettings["mailAccount"],
  84. ConfigurationManager.AppSettings["mailPassword"]
  85. );
  86.  
  87. // Create a Web transport for sending email.
  88. var transportWeb = new Web(credentials);
  89.  
  90. // Send the email.
  91. if (transportWeb != null)
  92. {
  93. await transportWeb.DeliverAsync(myMessage);
  94. }
  95. else
  96. {
  97. Trace.TraceError("Failed to create Web transport.");
  98. await Task.FromResult(0);
  99. }
  100. }
  101. }
  102.  
  103. public class SmsService : IIdentityMessageService
  104. {
  105. public Task SendAsync(IdentityMessage message)
  106. {
  107. // Plug in your sms service here to send a text message.
  108. return Task.FromResult(0);
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement