Advertisement
Guest User

Untitled

a guest
Jul 17th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 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.  
  76.  
  77.  
  78. [AllowAnonymous]
  79. public ActionResult Register()
  80. {
  81. // Remove the Cookie in Here as he goes a bit further
  82. if (ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("FinancesModelDataCookie"))
  83. {
  84. HttpCookie cookie = ControllerContext.HttpContext.Request.Cookies["FinancesModelDataCookie"];
  85. if (cookie != null)
  86. {
  87. cookie.Expires = DateTime.Now.AddDays(-1);
  88. ControllerContext.HttpContext.Response.Cookies.Add(cookie);
  89. }
  90. }
  91. return View();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement