Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Course-Management-System
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Laxmana Arasavilli
Course-Management-System
Merge requests
!6
Authentication added & role based authorization implemented
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Authentication added & role based authorization implemented
master
into
main
Overview
0
Commits
3
Pipelines
0
Changes
13
Merged
Laxmana Arasavilli
requested to merge
master
into
main
5 months ago
Overview
0
Commits
3
Pipelines
0
Changes
13
Expand
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
2b35c407
3 commits,
5 months ago
13 files
+
153
−
93
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
13
Search (e.g. *.vue) (Ctrl+P)
Course-Management-System.API/Controllers/AuthenticationController.cs
+
15
−
73
Options
using
Course_Management_System.Database.Entities
;
using
Course_Management_System.Services.DTO.RequestDTO
;
using
Course_Management_System.Services.Interfaces
;
using
Course_Management_System.Utility.CustomResponseTypes
;
using
Microsoft.AspNetCore.Authorization
;
using
Microsoft.AspNetCore.Identity
;
using
Microsoft.AspNetCore.Mvc
;
namespace
Course_Management_System.API.Controllers
;
@@ -10,89 +11,30 @@ namespace Course_Management_System.API.Controllers;
[
Route
(
"[controller]/[Action]"
)]
public
class
AuthenticationController
:
ControllerBase
{
private
readonly
UserManager
<
UserEntity
>
_userManager
;
//signInManager will hold the SignInManager instance
private
readonly
SignInManager
<
UserEntity
>
_signInManager
;
//Both UserManager and SignInManager services are injected into the AccountController
//using constructor injection
public
AuthenticationController
(
UserManager
<
UserEntity
>
userManager
,
SignInManager
<
UserEntity
>
signInManager
)
private
readonly
IAuthenticationServices
_authenticationServices
;
public
AuthenticationController
(
IAuthenticationServices
authenticationServices
)
{
_userManager
=
userManager
;
_signInManager
=
signInManager
;
_authenticationServices
=
authenticationServices
;
}
[
HttpPost
]
public
async
Task
<
IActionResult
>
Register
([
FromForm
]
UserRegisterRequestDTO
newUser
)
[
AllowAnonymous
]
public
async
Task
<
CustomResponse
>
Register
([
FromForm
]
UserRegisterRequestDTO
newUser
)
{
if
(
ModelState
.
IsValid
)
{
// Copy data from RegisterViewModel to UserEntity
var
user
=
new
UserEntity
{
UserName
=
newUser
.
UserName
,
Email
=
newUser
.
Email
,
CreatedAt
=
DateTime
.
Now
,
UpdatedAt
=
DateTime
.
Now
,
UpdatedBy
=
$"
{
newUser
.
UserName
}
"
,
CreatedBy
=
$"
{
newUser
.
UserName
}
"
,
IsActive
=
false
,
RoleType
=
newUser
.
RoleType
};
// Store user data in AspNetUsers database table
var
result
=
await
_userManager
.
CreateAsync
(
user
,
newUser
.
Password
);
// If user is successfully created, sign-in the user using
// SignInManager and redirect to index action of HomeController
if
(
result
.
Succeeded
)
{
await
_signInManager
.
SignInAsync
(
user
,
isPersistent
:
false
);
await
_userManager
.
AddToRoleAsync
(
user
,
newUser
.
RoleType
.
ToString
());
return
Ok
(
"Registered successfully"
);
}
// If there are any errors, add them to the ModelState object
// which will be displayed by the validation summary tag helper
foreach
(
var
error
in
result
.
Errors
)
{
ModelState
.
AddModelError
(
string
.
Empty
,
error
.
Description
);
}
}
return
BadRequest
();
return
await
_authenticationServices
.
RegisterNewUser
(
newUser
);
}
[
HttpPost
]
[
AllowAnonymous
]
public
async
Task
<
IActionResult
>
Login
([
FromForm
]
UserLoginRequestDTO
userToLogin
)
public
async
Task
<
CustomResponse
>
Login
([
FromForm
]
UserLoginRequestDTO
userToLogin
)
{
if
(
ModelState
.
IsValid
)
{
var
result
=
await
_signInManager
.
PasswordSignInAsync
(
userToLogin
.
UserName
,
userToLogin
.
Password
,
userToLogin
.
RememberMe
,
lockoutOnFailure
:
false
);
if
(
result
.
Succeeded
)
{
// Handle successful login
// return RedirectToAction(nameof(HomeController.Index), "Home");
var
loggedInUser
=
_userManager
.
Users
.
Where
(
u
=>
u
.
UserName
==
userToLogin
.
UserName
).
FirstOrDefault
();
loggedInUser
.
IsActive
=
true
;
await
_userManager
.
UpdateAsync
(
loggedInUser
);
return
Ok
(
"Logged in successfully"
);
}
else
{
// Handle failure
ModelState
.
AddModelError
(
string
.
Empty
,
"Invalid login attempt."
);
return
BadRequest
();
}
}
return
Ok
();
return
await
_authenticationServices
.
LoginUser
(
userToLogin
);
}
[
HttpPost
]
public
async
Task
<
IActionResult
>
Logout
()
{
var
loggedInUser
=
_userManager
.
Users
.
Where
(
u
=>
u
.
IsActive
==
true
).
FirstOrDefault
();
loggedInUser
.
IsActive
=
false
;
await
_userManager
.
UpdateAsync
(
loggedInUser
);
await
_signInManager
.
SignOutAsync
();
return
Ok
(
"Logout Successfully"
);
[
AllowAnonymous
]
public
async
Task
<
CustomResponse
>
Logout
()
{
return
await
_authenticationServices
.
LogOutUser
();
}
}
\ No newline at end of file
Loading