Advertisement
Guest User

Untitled

a guest
Jul 11th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. [assembly: OwinStartup(typeof(IoTWeb.App_Start.Startup))]
  2.  
  3. namespace IoTWeb.App_Start
  4. {
  5. public class Startup
  6. {
  7. private const int DEFAULTTIMEOUT = 5;
  8. private const int DEFAULTEXPIRETIMESPAN = 5;
  9. public void Configuration(IAppBuilder app)
  10. {
  11. // Enable the application to use a cookie to store information for the signed in user
  12. app.UseCookieAuthentication(new CookieAuthenticationOptions
  13. {
  14. AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
  15. LoginPath = new PathString("/Account/Login")
  16. });
  17.  
  18. app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
  19.  
  20. // Enable the application to use a cookie to store information for the signed in user
  21. // and to use a cookie to temporarily store information about a user logging in with a third party login provider
  22. // Configure the sign in cookie
  23. app.UseCookieAuthentication(new CookieAuthenticationOptions
  24. {
  25. AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
  26. LoginPath = new PathString("/Account/Login"),
  27. Provider = new CookieAuthenticationProvider
  28. {
  29. OnValidateIdentity =
  30. SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, User>(TimeSpan.FromMinutes(DEFAULTTIMEOUT),
  31. (manager, user) => Task.FromResult(manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie)))
  32. },
  33. SlidingExpiration = true,
  34. ExpireTimeSpan = TimeSpan.FromMinutes(DEFAULTEXPIRETIMESPAN)
  35. });
  36.  
  37. // Use a cookie to temporarily store information about a user logging in with a third party login provider
  38. app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
  39. }
  40. }
  41.  
  42. public class UserStoreService : IUserStore<User>, IUserPasswordStore<User>, IUserEmailStore<User>
  43. {
  44. private readonly TenantEntities context = new TenantEntities();
  45.  
  46. public Task<User> Find(string userName, string password)
  47. {
  48. Task<User> task = context.User.Where(
  49. apu => apu.UserName == userName && apu.Password == password)
  50. .FirstOrDefaultAsync();
  51. return task;
  52. }
  53.  
  54. public ActionResult Login(LoginViewModel model, string returnUrl)
  55. {
  56. if (ModelState.IsValid)
  57. {
  58. var manager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
  59. User user = manager.Find(model.UserName, model.Password);
  60. if (user != null)
  61. {
  62. IAuthenticationManager authenticationManager = HttpContext.GetOwinContext().Authentication;
  63. authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
  64. ClaimsIdentity identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
  65. authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, identity);
  66. return RedirectToLocal(returnUrl);
  67. }
  68. }
  69.  
  70. // If we got this far, something failed, redisplay form
  71. ModelState.AddModelError("", "Login failed due to incorrect credentials.");
  72. return View(model);
  73. }
  74.  
  75. [AllowAnonymous]
  76. public ActionResult Register()
  77. {
  78. // Remove the Cookie in Here as he goes a bit further
  79. if (ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("FinancesModelDataCookie"))
  80. {
  81. HttpCookie cookie = ControllerContext.HttpContext.Request.Cookies["FinancesModelDataCookie"];
  82. if (cookie != null)
  83. {
  84. cookie.Expires = DateTime.Now.AddDays(-1);
  85. ControllerContext.HttpContext.Response.Cookies.Add(cookie);
  86. }
  87. }
  88. return View();
  89. }
  90.  
  91. User user = manager.Find(model.UserName, model.Password);
  92.  
  93. public class ApplicationUserManager : UserManager<User>
  94. {
  95. public ApplicationUserManager()
  96. : base(new UserStoreService())
  97. {
  98. }
  99.  
  100. public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
  101. IOwinContext context)
  102. {
  103. var manager = new ApplicationUserManager();
  104. manager.PasswordHasher = new PasswordHasher(); // new NoPasswordHasher();
  105.  
  106.  
  107. // Configure validation logic for usernames
  108. manager.UserValidator = new UserValidator<User>(manager)
  109. {
  110. AllowOnlyAlphanumericUserNames = false,
  111. RequireUniqueEmail = true
  112. };
  113.  
  114. //Configure validation logic for passwords
  115. manager.PasswordValidator = new PasswordValidator
  116. {
  117. RequiredLength = 6,
  118. RequireNonLetterOrDigit = true,
  119. RequireDigit = true,
  120. };
  121. manager.PasswordValidator = new CustomPasswordValidator(6); //commented for and used above defined validator
  122.  
  123. //manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<User>
  124. //{
  125. // Subject = "SecurityCode",
  126. // BodyFormat = "Your security code is: {0}"
  127. //});
  128. manager.EmailService = new EmailService();
  129.  
  130. IDataProtectionProvider dataProtectionProvider = options.DataProtectionProvider;
  131. if (dataProtectionProvider != null)
  132. {
  133. manager.UserTokenProvider = new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"));
  134. }
  135. return manager;
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement