George Williams 5 jaren geleden
bovenliggende
commit
51e97dacef

+ 14
- 0
UnivateProperties_API/Containers/Users/AgentDto.cs Bestand weergeven

1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Threading.Tasks;
5
+using UnivateProperties_API.Model.Users;
6
+
7
+namespace UnivateProperties_API.Containers.Users
8
+{
9
+    public class AgentDto
10
+    {
11
+        public Agent Agent { get; set; }
12
+        public string Password { get; set; }
13
+    }
14
+}

+ 10
- 4
UnivateProperties_API/Controllers/Users/AgentController.cs Bestand weergeven

1
 using System.Transactions;
1
 using System.Transactions;
2
 using Microsoft.AspNetCore.Mvc;
2
 using Microsoft.AspNetCore.Mvc;
3
+using UnivateProperties_API.Containers.Users;
4
+using UnivateProperties_API.Helpers;
3
 using UnivateProperties_API.Model.Users;
5
 using UnivateProperties_API.Model.Users;
4
 using UnivateProperties_API.Repository;
6
 using UnivateProperties_API.Repository;
5
-using UnivateProperties_API.Repository.Users;
6
 
7
 
7
 namespace User_API.Controllers
8
 namespace User_API.Controllers
8
 {
9
 {
30
         }
31
         }
31
 
32
 
32
         [HttpPost()]
33
         [HttpPost()]
33
-        public IActionResult Post([FromBody] Agent agent)
34
+        public IActionResult Post([FromBody] AgentDto agentDto)
34
         {
35
         {
35
             using (var scope = new TransactionScope())
36
             using (var scope = new TransactionScope())
36
             {
37
             {
37
-                
38
+                Agent agent = agentDto.Agent;
39
+                byte[] passwordHash, passwordSalt;
40
+                MyCommon.CreatePasswordHash(agentDto.Password, out passwordHash, out passwordSalt);
41
+
42
+                agent.User.PasswordHash = passwordHash;
43
+                agent.User.PasswordSalt = passwordSalt;
38
                 _Repo.Insert(agent);
44
                 _Repo.Insert(agent);
39
                 scope.Complete();
45
                 scope.Complete();
40
-                return CreatedAtAction(nameof(Get), new { id = agent.Id }, agent);
46
+                return CreatedAtAction(nameof(Get), new { id = agentDto.Agent.Id }, agentDto.Agent);
41
             }
47
             }
42
         }
48
         }
43
 
49
 

+ 6
- 2
UnivateProperties_API/Controllers/Users/RegisterController.cs Bestand weergeven

15
 using UnivateProperties_API.Model.Users;
15
 using UnivateProperties_API.Model.Users;
16
 using UnivateProperties_API.Repository;
16
 using UnivateProperties_API.Repository;
17
 using UnivateProperties_API.Repository.Users;
17
 using UnivateProperties_API.Repository.Users;
18
+using System.Net.Http;
19
+using System.Net;
20
+using System.Web.Http;
18
 
21
 
19
 namespace UnivateProperties_API.Controllers.Users
22
 namespace UnivateProperties_API.Controllers.Users
20
 {
23
 {
40
         {
43
         {
41
             var user = _Repo.Authenticate(userDto.Username, userDto.Password);
44
             var user = _Repo.Authenticate(userDto.Username, userDto.Password);
42
 
45
 
46
+            //HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Unauthorized, "value");
47
+
43
             if (user == null)
48
             if (user == null)
44
                 return BadRequest(new { message = "Username or password is incorrect" });
49
                 return BadRequest(new { message = "Username or password is incorrect" });
45
 
50
 
56
                 SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
61
                 SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
57
             };
62
             };
58
             var token = tokenHandler.CreateToken(tokenDescriptor);
63
             var token = tokenHandler.CreateToken(tokenDescriptor);
59
-            var tokenString = tokenHandler.WriteToken(token); 
60
-
64
+            var tokenString = tokenHandler.WriteToken(token);
61
             // return basic user info (without password) and token to store client side
65
             // return basic user info (without password) and token to store client side
62
             return Ok(new
66
             return Ok(new
63
             {
67
             {

+ 33
- 1
UnivateProperties_API/Helpers/MyCommon.cs Bestand weergeven

1
-using System.Text.RegularExpressions;
1
+using System;
2
+using System.Text.RegularExpressions;
2
 
3
 
3
 namespace UnivateProperties_API.Helpers
4
 namespace UnivateProperties_API.Helpers
4
 {
5
 {
14
             }
15
             }
15
             else return false;
16
             else return false;
16
         }
17
         }
18
+
19
+        public static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
20
+        {
21
+            if (password == null) throw new ArgumentNullException("password");
22
+            if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
23
+
24
+            using (var hmac = new System.Security.Cryptography.HMACSHA512())
25
+            {
26
+                passwordSalt = hmac.Key;
27
+                passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
28
+            }
29
+        }
30
+
31
+        public static bool VerifyPasswordHash(string password, byte[] storedHash, byte[] storedSalt)
32
+        {
33
+            if (password == null) throw new ArgumentNullException("password");
34
+            if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
35
+            if (storedHash.Length != 64) throw new ArgumentException("Invalid length of password hash (64 bytes expected).", "passwordHash");
36
+            if (storedSalt.Length != 128) throw new ArgumentException("Invalid length of password salt (128 bytes expected).", "passwordHash");
37
+
38
+            using (var hmac = new System.Security.Cryptography.HMACSHA512(storedSalt))
39
+            {
40
+                var computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
41
+                for (int i = 0; i < computedHash.Length; i++)
42
+                {
43
+                    if (computedHash[i] != storedHash[i]) return false;
44
+                }
45
+            }
46
+
47
+            return true;
48
+        }
17
     }
49
     }
18
 }
50
 }

+ 1
- 1
UnivateProperties_API/Model/Users/Person.cs Bestand weergeven

16
 
16
 
17
         #region Properties
17
         #region Properties
18
         [ForeignKey("User")]
18
         [ForeignKey("User")]
19
-        public int UserId { get; set; }
19
+        public int? UserId { get; set; }
20
         public string Name { get; set; }
20
         public string Name { get; set; }
21
         public string Surname { get; set; }
21
         public string Surname { get; set; }
22
         public string Email { get; set; }
22
         public string Email { get; set; }

+ 17
- 1
UnivateProperties_API/Model/Users/User.cs Bestand weergeven

1
-namespace UnivateProperties_API.Model.Users
1
+using UnivateProperties_API.Helpers;
2
+
3
+namespace UnivateProperties_API.Model.Users
2
 {
4
 {
3
     public class User : BaseEntity
5
     public class User : BaseEntity
4
     {
6
     {
5
         #region Constructor
7
         #region Constructor
8
+        public User(string username, string password)
9
+        {
10
+            Username = username;
11
+
12
+            byte[] passwordHash, passwordSalt;
13
+            MyCommon.CreatePasswordHash(password, out passwordHash, out passwordSalt);
14
+
15
+            PasswordHash = passwordHash;
16
+            PasswordSalt = passwordSalt;
17
+        }
18
+
19
+        /// <summary>
20
+        /// Do not use when creating new user
21
+        /// </summary>
6
         public User()
22
         public User()
7
         {
23
         {
8
 
24
 

+ 2
- 5
UnivateProperties_API/Repository/Users/AgentRepository.cs Bestand weergeven

3
 using System.Collections.Generic;
3
 using System.Collections.Generic;
4
 using System.Linq;
4
 using System.Linq;
5
 using UnivateProperties_API.Context;
5
 using UnivateProperties_API.Context;
6
+using UnivateProperties_API.Helpers;
6
 using UnivateProperties_API.Model.Users;
7
 using UnivateProperties_API.Model.Users;
7
 
8
 
8
 namespace UnivateProperties_API.Repository.Users
9
 namespace UnivateProperties_API.Repository.Users
28
 
29
 
29
         public Agent GetDetailed(Func<Agent, bool> first)
30
         public Agent GetDetailed(Func<Agent, bool> first)
30
         {
31
         {
32
+
31
             var item = _dbContext.Agents.FirstOrDefault(first);
33
             var item = _dbContext.Agents.FirstOrDefault(first);
32
             //AgentRepository account = new AgentRepository(_dbContext);
34
             //AgentRepository account = new AgentRepository(_dbContext);
33
             //item = GetDetailedObject(item, account);
35
             //item = GetDetailedObject(item, account);
42
 
44
 
43
         public void Insert(Agent item)
45
         public void Insert(Agent item)
44
         {
46
         {
45
-            if (item.AgencyId != 0 && item.Agency == null)
46
-            {
47
-                AgencyRepository arepo = new AgencyRepository(_dbContext);
48
-                item.Agency = arepo.Get(a => a.Id == item.AgencyId).FirstOrDefault();
49
-            }
50
             _dbContext.Add(item);
47
             _dbContext.Add(item);
51
             Save();
48
             Save();
52
         }
49
         }

+ 9
- 46
UnivateProperties_API/Repository/Users/RegisterRepository.cs Bestand weergeven

37
 
37
 
38
             // check if username exists
38
             // check if username exists
39
             if (user == null)
39
             if (user == null)
40
-                return null;
40
+                throw new AppException("Username is incorrect");
41
 
41
 
42
             // check if password is correct
42
             // check if password is correct
43
-            if (!VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt))
44
-                return null;
43
+            if (!MyCommon.VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt))
44
+                throw new AppException("Password is incorrect");
45
 
45
 
46
             // authentication successful
46
             // authentication successful
47
             return user;
47
             return user;
57
                 throw new AppException("Username \"" + user.Username + "\" is already taken");
57
                 throw new AppException("Username \"" + user.Username + "\" is already taken");
58
 
58
 
59
             byte[] passwordHash, passwordSalt;
59
             byte[] passwordHash, passwordSalt;
60
-            CreatePasswordHash(password, out passwordHash, out passwordSalt);
60
+            MyCommon.CreatePasswordHash(password, out passwordHash, out passwordSalt);
61
 
61
 
62
             user.PasswordHash = passwordHash;
62
             user.PasswordHash = passwordHash;
63
             user.PasswordSalt = passwordSalt;
63
             user.PasswordSalt = passwordSalt;
105
                 throw new AppException("Individual \"" + individual.Username + "\" is already taken");
105
                 throw new AppException("Individual \"" + individual.Username + "\" is already taken");
106
             byte[] passwordHash, passwordSalt;
106
             byte[] passwordHash, passwordSalt;
107
 
107
 
108
-            CreatePasswordHash(individual.Password, out passwordHash, out passwordSalt);
108
+            MyCommon.CreatePasswordHash(individual.Password, out passwordHash, out passwordSalt);
109
+
110
+            User createUser = new User(individual.Username, individual.Password);
109
 
111
 
110
-            User createUser = new User()
111
-            {
112
-                Username = individual.Username,
113
-                PasswordHash = passwordHash,
114
-                PasswordSalt = passwordSalt
115
-            };
116
             Create(createUser, individual.Password, save);
112
             Create(createUser, individual.Password, save);
117
-            Person person = new Person()
118
-            {
119
-            };
113
+
120
             if (personType == PersonType.Agent)
114
             if (personType == PersonType.Agent)
121
             {
115
             {
122
                 Agent agent = new Agent()
116
                 Agent agent = new Agent()
171
             if (!string.IsNullOrWhiteSpace(password))
165
             if (!string.IsNullOrWhiteSpace(password))
172
             {
166
             {
173
                 byte[] passwordHash, passwordSalt;
167
                 byte[] passwordHash, passwordSalt;
174
-                CreatePasswordHash(password, out passwordHash, out passwordSalt);
168
+                MyCommon.CreatePasswordHash(password, out passwordHash, out passwordSalt);
175
 
169
 
176
                 user.PasswordHash = passwordHash;
170
                 user.PasswordHash = passwordHash;
177
                 user.PasswordSalt = passwordSalt;
171
                 user.PasswordSalt = passwordSalt;
248
         {
242
         {
249
             _dbContext.SaveChanges();
243
             _dbContext.SaveChanges();
250
         }
244
         }
251
-
252
-        private static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
253
-        {
254
-            if (password == null) throw new ArgumentNullException("password");
255
-            if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
256
-
257
-            using (var hmac = new System.Security.Cryptography.HMACSHA512())
258
-            {
259
-                passwordSalt = hmac.Key;
260
-                passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
261
-            }
262
-        }
263
-
264
-        private static bool VerifyPasswordHash(string password, byte[] storedHash, byte[] storedSalt)
265
-        {
266
-            if (password == null) throw new ArgumentNullException("password");
267
-            if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
268
-            if (storedHash.Length != 64) throw new ArgumentException("Invalid length of password hash (64 bytes expected).", "passwordHash");
269
-            if (storedSalt.Length != 128) throw new ArgumentException("Invalid length of password salt (128 bytes expected).", "passwordHash");
270
-
271
-            using (var hmac = new System.Security.Cryptography.HMACSHA512(storedSalt))
272
-            {
273
-                var computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
274
-                for (int i = 0; i < computedHash.Length; i++)
275
-                {
276
-                    if (computedHash[i] != storedHash[i]) return false;
277
-                }
278
-            }
279
-
280
-            return true;
281
-        }
282
     }
245
     }
283
 }
246
 }

Laden…
Annuleren
Opslaan