Guest User

Untitled

a guest
Feb 1st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. [MobileAppController]
  2. [RoutePrefix("api/register")]
  3. [AllowAnonymous]
  4. public class RegisterController : ApiController
  5. {
  6.  
  7. [HttpPost]
  8. [Route("newuser")]
  9. public HttpResponseMessage NewUser(RegistrationRequest request)
  10. {
  11. try
  12. {
  13.  
  14. //...... code truncated for brevity
  15.  
  16. myContext context = new myContext();
  17. DataObjects.Account account = context.Accounts.Where(a => a.Username == request.username).SingleOrDefault();
  18.  
  19. if (account != null)
  20. {
  21. return Request.CreateResponse(HttpStatusCode.OK, new
  22. {
  23. message = "That username already exists",
  24. error = true
  25. });
  26.  
  27. }
  28. else
  29. {
  30. byte[] salt = CustomLoginProviderUtils.generateSalt();
  31.  
  32. DataObjects.Account newAccount = new DataObjects.Account()
  33. {
  34. Id = Guid.NewGuid().ToString(),
  35. Username = request.username,
  36. Salt = salt,
  37. CreatedAt = DateTime.UtcNow,
  38. SaltedAndHashedPassword = CustomLoginProviderUtils.hash(request.password, salt)
  39. };
  40. context.Accounts.Add(newAccount);
  41. context.SaveChanges();
  42.  
  43. return Request.CreateResponse(HttpStatusCode.OK, new
  44. {
  45. message = "Account created successfully!",
  46. error = false,
  47. account = newAccount
  48. });
  49. }
  50.  
  51.  
  52. }catch(Exception ex)
  53. {
  54. // Retrieve the error messages as a list of strings.
  55. //var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);
  56.  
  57. //// Join the list to a single string.
  58. //var fullErrorMessage = string.Join(";", errorMessages);
  59.  
  60. //// Combine the original exception message with the new one.
  61. //var exceptionMessage = string.Concat(ex.Message, "The validation errors are:", fullErrorMessage);
  62.  
  63. // Throw a new DbEntityValidationException with the improved exception message.
  64. //throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
  65.  
  66. return Request.CreateResponse(HttpStatusCode.OK, new
  67. {
  68. message = "Failed!",
  69. error = true
  70. });
  71. }
  72.  
  73. public override int SaveChanges()
  74. {
  75.  
  76. try
  77. {
  78. return base.SaveChanges();
  79.  
  80. }catch (DbEntityValidationException ex)
  81. {
  82. // Retrieve the error messages as a list of strings.
  83. var errorMessages = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);
  84.  
  85. // Join the list to a single string.
  86. var fullErrorMessage = string.Join(";", errorMessages);
  87.  
  88. // Combine the original exception message with the new one.
  89. var exceptionMessage = string.Concat(ex.Message, "The validation errors are:", fullErrorMessage);
  90.  
  91. // Throw a new DbEntityValidationException with the improved exception message.
  92. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
  93. }
  94. }
  95.  
  96. **Generate Salt function**
  97.  
  98. public static byte[] generateSalt()
  99. {
  100. RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  101. byte[] salt = new byte[256];
  102. rng.GetBytes(salt);
  103. //rng.GetNonZeroBytes(salt);
  104. return salt;
  105. }
Add Comment
Please, Sign In to add comment