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+ }
0 commit comments