Advertisement
Guest User

Untitled

a guest
Oct 9th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. using System;
  2. using CakesWebApp.Data;
  3. using Microsoft.EntityFrameworkCore.Internal;
  4. using SIS.HTTP.Enums;
  5. using SIS.HTTP.Requests;
  6. using SIS.HTTP.Responses;
  7. using SIS.WebServer.Results;
  8. using System.Linq;
  9. using CakesWebApp.Models;
  10. using CakesWebApp.Services;
  11. using SIS.HTTP.Cookies;
  12.  
  13. namespace CakesWebApp.Controllers
  14. {
  15. public class AccountController : BaseController
  16. {
  17. private IHashService hashService;
  18.  
  19. public AccountController()
  20. {
  21. this.hashService = new HashService();
  22. }
  23.  
  24. public IHttpResponse Register(IHttpRequest request)
  25. {
  26. return this.View("Register");
  27. }
  28.  
  29. public IHttpResponse DoRegister(IHttpRequest request)
  30. {
  31. var userName = request.FormData["username"].ToString().Trim();
  32. var password = request.FormData["password"].ToString();
  33. var confirmPassword = request.FormData["confirmPassword"].ToString();
  34.  
  35. // Validate
  36. if (string.IsNullOrWhiteSpace(userName) || userName.Length < 4)
  37. {
  38. return this.BadRequestError("Please provide valid username with length of 4 or more characters.");
  39. }
  40.  
  41. if (this.Db.Users.Any(x => x.Username == userName))
  42. {
  43. return this.BadRequestError("User with the same name already exists.");
  44. }
  45.  
  46. if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
  47. {
  48. return this.BadRequestError("Please provide password of length 6 or more.");
  49. }
  50.  
  51. if (password != confirmPassword)
  52. {
  53. return this.BadRequestError("Passwords do not match.");
  54. }
  55.  
  56. // Hash password
  57. var hashedPassword = this.hashService.Hash(password);
  58.  
  59. // Create user
  60. var user = new User
  61. {
  62. Name = userName,
  63. Username = userName,
  64. Password = hashedPassword,
  65. };
  66. this.Db.Users.Add(user);
  67.  
  68. try
  69. {
  70. this.Db.SaveChanges();
  71. }
  72. catch (Exception e)
  73. {
  74. // TODO: Log error
  75. return this.ServerError(e.Message);
  76. }
  77.  
  78. // TODO: Login
  79.  
  80. // Redirect
  81. return new RedirectResult("/");
  82. }
  83.  
  84. public IHttpResponse Login(IHttpRequest request)
  85. {
  86. return this.View("Login");
  87. }
  88.  
  89. public IHttpResponse DoLogin(IHttpRequest request)
  90. {
  91. var userName = request.FormData["username"].ToString().Trim();
  92. var password = request.FormData["password"].ToString();
  93.  
  94. var hashedPassword = this.hashService.Hash(password);
  95.  
  96. var user = this.Db.Users.FirstOrDefault(x =>
  97. x.Username == userName &&
  98. x.Password == hashedPassword);
  99.  
  100. if (user == null)
  101. {
  102. return this.BadRequestError("Invalid username or password.");
  103. }
  104.  
  105. var cookieContent = this.UserCookieService.GetUserCookie(user.Username);
  106.  
  107. var response = new RedirectResult("/");
  108. var cookie = new HttpCookie(".auth-cakes", cookieContent, 7) { HttpOnly = true };
  109. response.Cookies.Add(cookie);
  110. return response;
  111. }
  112.  
  113. public IHttpResponse Logout(IHttpRequest request)
  114. {
  115. if (!request.Cookies.ContainsCookie(".auth-cakes"))
  116. {
  117. return new RedirectResult("/");
  118. }
  119.  
  120. var cookie = request.Cookies.GetCookie(".auth-cakes");
  121. cookie.Delete();
  122. var response = new RedirectResult("/");
  123. response.Cookies.Add(cookie);
  124. return response;
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement