Skip to content

Commit d41838b

Browse files
author
George Sovatzis
committed
Add project files.
1 parent 251e8df commit d41838b

22 files changed

+1782
-0
lines changed

WebAPI_Identity.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.572
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAPI_Identity", "WebAPI_Identity\WebAPI_Identity.csproj", "{60EF1BED-64F7-4C52-AA46-AADA7A8F2F95}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{60EF1BED-64F7-4C52-AA46-AADA7A8F2F95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{60EF1BED-64F7-4C52-AA46-AADA7A8F2F95}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{60EF1BED-64F7-4C52-AA46-AADA7A8F2F95}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{60EF1BED-64F7-4C52-AA46-AADA7A8F2F95}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {1CC227A3-B878-4BF1-B42C-E37ECE9014E8}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Authorization;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Mvc;
8+
using WebAPI_Identity.Data;
9+
using WebAPI_Identity.Models;
10+
11+
namespace WebAPI_Identity.Controllers
12+
{
13+
[Authorize]
14+
[Route("[controller]")]
15+
[ApiController]
16+
public class ProductsController : ControllerBase
17+
{
18+
private ApplicationDbContext _context;
19+
20+
public ProductsController(ApplicationDbContext context)
21+
{
22+
this._context = context;
23+
}
24+
25+
// GET: api/Products
26+
[HttpGet]
27+
public IEnumerable<Product> Get()
28+
{
29+
var products = _context.products;
30+
31+
return products;
32+
}
33+
34+
// GET: api/Products/5
35+
[HttpGet("{id}", Name = "Get")]
36+
public string Get(int id)
37+
{
38+
return "value";
39+
}
40+
41+
// POST: api/Products
42+
[HttpPost]
43+
public Product Post([FromBody] Product value)
44+
{
45+
_context.Add(value);
46+
_context.SaveChanges();
47+
48+
return value;
49+
}
50+
51+
// PUT: api/Products/5
52+
[HttpPut("{id}")]
53+
public void Put(int id, [FromBody] Product value)
54+
{
55+
_context.Update(value);
56+
_context.SaveChanges();
57+
}
58+
59+
// DELETE: api/ApiWithActions/5
60+
[HttpDelete("{id}")]
61+
public void Delete(int id)
62+
{
63+
}
64+
}
65+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IdentityModel.Tokens.Jwt;
4+
using System.Linq;
5+
using System.Security.Claims;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using AutoMapper;
9+
using Microsoft.AspNetCore.Authorization;
10+
using Microsoft.AspNetCore.Identity;
11+
using Microsoft.AspNetCore.Mvc;
12+
using Microsoft.Extensions.Options;
13+
using Microsoft.IdentityModel.Tokens;
14+
using WebAPI_Identity.DTOs;
15+
using WebAPI_Identity.Helpers;
16+
using WebAPI_Identity.Models;
17+
using WebAPI_Identity.Services;
18+
19+
namespace WebAPI_Identity.Controllers
20+
{
21+
[Authorize]
22+
[ApiController]
23+
[Route("[controller]")]
24+
public class UsersController : Controller
25+
{
26+
private IUserService _userService;
27+
28+
private IMapper _mapper;
29+
private readonly AppSettings _appSettings;
30+
31+
public UsersController(
32+
IUserService userService,
33+
IMapper mapper,
34+
IOptions<AppSettings> appSettings)
35+
{
36+
_userService = userService;
37+
_mapper = mapper;
38+
_appSettings = appSettings.Value;
39+
}
40+
41+
[AllowAnonymous]
42+
[HttpPost("authenticate")]
43+
public IActionResult Authenticate([FromBody]UserDTO UserDTO)
44+
{
45+
var user = _userService.Authenticate(UserDTO.UserName, UserDTO.Password);
46+
47+
if (user == null)
48+
return BadRequest(new { message = "Username or password is incorrect" });
49+
50+
51+
52+
var tokenHandler = new JwtSecurityTokenHandler();
53+
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
54+
var tokenDescriptor = new SecurityTokenDescriptor
55+
{
56+
Subject = new ClaimsIdentity(new Claim[]
57+
{
58+
new Claim(ClaimTypes.Name, user.Id.ToString())
59+
}),
60+
Expires = DateTime.UtcNow.AddDays(7),
61+
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
62+
};
63+
var token = tokenHandler.CreateToken(tokenDescriptor);
64+
var tokenString = tokenHandler.WriteToken(token);
65+
66+
// return basic user info (without password) and token to store client side
67+
return Ok(new
68+
{
69+
Id = user.Id,
70+
Username = user.UserName,
71+
FirstName = user.FirstName,
72+
LastName = user.LastName,
73+
Token = tokenString
74+
});
75+
}
76+
77+
[AllowAnonymous]
78+
[HttpPost("register")]
79+
public IActionResult Register([FromBody]UserDTO UserDTO)
80+
{
81+
// map dto to entity
82+
var user = _mapper.Map<MyUser>(UserDTO);
83+
84+
try
85+
{
86+
// save
87+
var result = _userService.Create(user, UserDTO.Password);
88+
return Ok();
89+
}
90+
catch (Exception ex)
91+
{
92+
// return error message if there was an exception
93+
return BadRequest(new { message = ex.Message });
94+
}
95+
}
96+
97+
[HttpGet]
98+
public IActionResult GetAll()
99+
{
100+
var users = _userService.GetAll();
101+
var UserDTOs = _mapper.Map<IList<UserDTO>>(users);
102+
return Ok(UserDTOs);
103+
}
104+
105+
[HttpGet("{id}")]
106+
public IActionResult GetById(string id)
107+
{
108+
var user = _userService.GetById(id);
109+
var UserDTO = _mapper.Map<UserDTO>(user);
110+
return Ok(UserDTO);
111+
}
112+
113+
[HttpPut("{id}")]
114+
public IActionResult Update(string id, [FromBody]UserDTO UserDTO)
115+
{
116+
// map dto to entity and set id
117+
var user = _mapper.Map<MyUser>(UserDTO);
118+
user.Id = id;
119+
120+
try
121+
{
122+
// save
123+
_userService.Update(user, UserDTO.CurrentPass, UserDTO.Password);
124+
return Ok();
125+
}
126+
catch (Exception ex)
127+
{
128+
// return error message if there was an exception
129+
return BadRequest(new { message = ex.Message });
130+
}
131+
}
132+
133+
[HttpDelete("{id}")]
134+
public IActionResult Delete(string id)
135+
{
136+
_userService.Delete(id);
137+
return Ok();
138+
}
139+
}
140+
141+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace WebAPI_Identity.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
[ApiController]
11+
public class ValuesController : ControllerBase
12+
{
13+
// GET api/values
14+
[HttpGet]
15+
public ActionResult<IEnumerable<string>> Get()
16+
{
17+
return new string[] { "value1", "value2" };
18+
}
19+
20+
// GET api/values/5
21+
[HttpGet("{id}")]
22+
public ActionResult<string> Get(int id)
23+
{
24+
return "value";
25+
}
26+
27+
// POST api/values
28+
[HttpPost]
29+
public void Post([FromBody] string value)
30+
{
31+
}
32+
33+
// PUT api/values/5
34+
[HttpPut("{id}")]
35+
public void Put(int id, [FromBody] string value)
36+
{
37+
}
38+
39+
// DELETE api/values/5
40+
[HttpDelete("{id}")]
41+
public void Delete(int id)
42+
{
43+
}
44+
}
45+
}

WebAPI_Identity/DTOs/UserDTO.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace WebAPI_Identity.DTOs
7+
{
8+
public class UserDTO
9+
{
10+
// This is an object to be user from WebAPI body -> it will be mapped with MyUser through Automapper
11+
public string Id { get; set; }
12+
public string FirstName { get; set; }
13+
public string LastName { get; set; }
14+
public string UserName { get; set; }
15+
public string Email { get; set; }
16+
public string CurrentPass { get; set; }
17+
public string Password { get; set; }
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore;
3+
using WebAPI_Identity.Models;
4+
5+
namespace WebAPI_Identity.Data
6+
{
7+
public class ApplicationDbContext : IdentityDbContext<MyUser>
8+
{
9+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
10+
: base(options)
11+
{
12+
13+
}
14+
15+
public DbSet<Product> products { get; set; }
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace WebAPI_Identity.Helpers
7+
{
8+
public class AppSettings
9+
{
10+
public string Secret { get; set; }
11+
}
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using AutoMapper;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using WebAPI_Identity.DTOs;
7+
using WebAPI_Identity.Models;
8+
9+
namespace WebAPI_Identity.Helpers
10+
{
11+
public class AutoMapperProfile : Profile
12+
{
13+
public AutoMapperProfile()
14+
{
15+
CreateMap<MyUser, UserDTO>();
16+
CreateMap<UserDTO, MyUser>();
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)