Kaynağa Gözat

Adding Agent on Agency

master
LeneS 5 yıl önce
ebeveyn
işleme
aee5e0268e

+ 14
- 0
UnivateProperties_API/Containers/Users/AgentDto.cs Dosyayı Görüntüle

@@ -0,0 +1,14 @@
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 Dosyayı Görüntüle

@@ -1,8 +1,9 @@
1 1
 using System.Transactions;
2 2
 using Microsoft.AspNetCore.Mvc;
3
+using UnivateProperties_API.Containers.Users;
4
+using UnivateProperties_API.Helpers;
3 5
 using UnivateProperties_API.Model.Users;
4 6
 using UnivateProperties_API.Repository;
5
-using UnivateProperties_API.Repository.Users;
6 7
 
7 8
 namespace User_API.Controllers
8 9
 {
@@ -30,14 +31,19 @@ namespace User_API.Controllers
30 31
         }
31 32
 
32 33
         [HttpPost()]
33
-        public IActionResult Post([FromBody] Agent agent)
34
+        public IActionResult Post([FromBody] AgentDto agentDto)
34 35
         {
35 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 44
                 _Repo.Insert(agent);
39 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 Dosyayı Görüntüle

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

+ 33
- 1
UnivateProperties_API/Helpers/MyCommon.cs Dosyayı Görüntüle

@@ -1,4 +1,5 @@
1
-using System.Text.RegularExpressions;
1
+using System;
2
+using System.Text.RegularExpressions;
2 3
 
3 4
 namespace UnivateProperties_API.Helpers
4 5
 {
@@ -14,5 +15,36 @@ namespace UnivateProperties_API.Helpers
14 15
             }
15 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 Dosyayı Görüntüle

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

+ 17
- 1
UnivateProperties_API/Model/Users/User.cs Dosyayı Görüntüle

@@ -1,8 +1,24 @@
1
-namespace UnivateProperties_API.Model.Users
1
+using UnivateProperties_API.Helpers;
2
+
3
+namespace UnivateProperties_API.Model.Users
2 4
 {
3 5
     public class User : BaseEntity
4 6
     {
5 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 22
         public User()
7 23
         {
8 24
 

+ 2
- 5
UnivateProperties_API/Repository/Users/AgentRepository.cs Dosyayı Görüntüle

@@ -3,6 +3,7 @@ using System;
3 3
 using System.Collections.Generic;
4 4
 using System.Linq;
5 5
 using UnivateProperties_API.Context;
6
+using UnivateProperties_API.Helpers;
6 7
 using UnivateProperties_API.Model.Users;
7 8
 
8 9
 namespace UnivateProperties_API.Repository.Users
@@ -28,6 +29,7 @@ namespace UnivateProperties_API.Repository.Users
28 29
 
29 30
         public Agent GetDetailed(Func<Agent, bool> first)
30 31
         {
32
+
31 33
             var item = _dbContext.Agents.FirstOrDefault(first);
32 34
             //AgentRepository account = new AgentRepository(_dbContext);
33 35
             //item = GetDetailedObject(item, account);
@@ -42,11 +44,6 @@ namespace UnivateProperties_API.Repository.Users
42 44
 
43 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 47
             _dbContext.Add(item);
51 48
             Save();
52 49
         }

+ 9
- 46
UnivateProperties_API/Repository/Users/RegisterRepository.cs Dosyayı Görüntüle

@@ -37,11 +37,11 @@ namespace UnivateProperties_API.Repository.Users
37 37
 
38 38
             // check if username exists
39 39
             if (user == null)
40
-                return null;
40
+                throw new AppException("Username is incorrect");
41 41
 
42 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 46
             // authentication successful
47 47
             return user;
@@ -57,7 +57,7 @@ namespace UnivateProperties_API.Repository.Users
57 57
                 throw new AppException("Username \"" + user.Username + "\" is already taken");
58 58
 
59 59
             byte[] passwordHash, passwordSalt;
60
-            CreatePasswordHash(password, out passwordHash, out passwordSalt);
60
+            MyCommon.CreatePasswordHash(password, out passwordHash, out passwordSalt);
61 61
 
62 62
             user.PasswordHash = passwordHash;
63 63
             user.PasswordSalt = passwordSalt;
@@ -105,18 +105,12 @@ namespace UnivateProperties_API.Repository.Users
105 105
                 throw new AppException("Individual \"" + individual.Username + "\" is already taken");
106 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 112
             Create(createUser, individual.Password, save);
117
-            Person person = new Person()
118
-            {
119
-            };
113
+
120 114
             if (personType == PersonType.Agent)
121 115
             {
122 116
                 Agent agent = new Agent()
@@ -171,7 +165,7 @@ namespace UnivateProperties_API.Repository.Users
171 165
             if (!string.IsNullOrWhiteSpace(password))
172 166
             {
173 167
                 byte[] passwordHash, passwordSalt;
174
-                CreatePasswordHash(password, out passwordHash, out passwordSalt);
168
+                MyCommon.CreatePasswordHash(password, out passwordHash, out passwordSalt);
175 169
 
176 170
                 user.PasswordHash = passwordHash;
177 171
                 user.PasswordSalt = passwordSalt;
@@ -248,36 +242,5 @@ namespace UnivateProperties_API.Repository.Users
248 242
         {
249 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
 }

Loading…
İptal
Kaydet