Skip to content

Commit

Permalink
Migrate IdentityServer4 -> Duende IdentityServer
Browse files Browse the repository at this point in the history
  • Loading branch information
phongnguyend committed Dec 28, 2024
1 parent 4288433 commit 1b2eaae
Show file tree
Hide file tree
Showing 345 changed files with 9,673 additions and 9,876 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using ClassifiedAds.Domain.Entities;
using ClassifiedAds.IdentityServer.Models.AccountModels;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace ClassifiedAds.IdentityServer.Controllers;

public class AccountController : Controller
{
private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;

public AccountController(UserManager<User> userManager,
SignInManager<User> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}

[HttpGet]
public async Task<IActionResult> ConfirmEmailAddress(string token, string email)
{
var user = await _userManager.FindByEmailAsync(email);

if (user == null)
{
return View("Error");
}

var result = await _userManager.ConfirmEmailAsync(user, token);

if (result.Succeeded)
{
return View("Success");
}

return View("Error");
}

[HttpGet]
public IActionResult ForgotPassword()
{
return View();
}

[HttpPost]
public async Task<IActionResult> ForgotPassword(ForgotPasswordModel model)
{
if (!ModelState.IsValid)
{
return View();
}

var user = await _userManager.FindByEmailAsync(model.Email);

if (user != null)
{
var token = await _userManager.GeneratePasswordResetTokenAsync(user);
var resetUrl = Url.Action("ResetPassword", "Account",
new { token = token, email = user.Email }, Request.Scheme);

//await _dispatcher.DispatchAsync(new AddOrUpdateEntityCommand<EmailMessage>(new EmailMessage
//{
// From = "[email protected]",
// Tos = user.Email,
// Subject = "Forgot Password",
// Body = string.Format("Reset Url: {0}", resetUrl),
//}));
}
else
{
// email user and inform them that they do not have an account
}

return View("Success");
}

[HttpGet]
public IActionResult ResetPassword(string token, string email)
{
return View(new ResetPasswordModel { Token = token, Email = email });
}

[HttpPost]
public async Task<IActionResult> ResetPassword(ResetPasswordModel model)
{
if (!ModelState.IsValid)
{
return View();
}

var user = await _userManager.FindByEmailAsync(model.Email);

if (user == null)
{
ModelState.AddModelError(string.Empty, "Invalid Request");
return View();
}

var result = await _userManager.ResetPasswordAsync(user, model.Token, model.Password);

if (result.Succeeded)
{
return View("Success");
}

foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}

return View();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace ClassifiedAds.IdentityServer.Models;
namespace ClassifiedAds.IdentityServer.Models.AccountModels;

public class ForgotPasswordModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace ClassifiedAds.IdentityServer.Models;
namespace ClassifiedAds.IdentityServer.Models.AccountModels;

public class RegisterModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace ClassifiedAds.IdentityServer.Models;
namespace ClassifiedAds.IdentityServer.Models.AccountModels;

public class ResetPasswordModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

<div class="alert alert-danger">
<p>Error!</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@using ClassifiedAds.IdentityServer.Models.AccountModels

@model ForgotPasswordModel

@await Html.PartialAsync("_ValidationSummary")

<h1>Forgot Password</h1>
<h4>Enter your email</h4>

<hr />
<div class="row">
<div class="col-md-4">
<form asp-controller="Account" asp-action="ForgotPassword" method="post">
<div class="form-group">
<label asp-for="Email">Email</label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@using ClassifiedAds.IdentityServer.Models.AccountModels

@model ResetPasswordModel

<h1>Reset Password</h1>

@await Html.PartialAsync("_ValidationSummary")

<form asp-controller="Account" asp-action="ResetPassword" method="post" class="form-horizontal">
<h4>Enter new password</h4>
<hr />
<input asp-for="Token" type="hidden" />
<input asp-for="Email" type="hidden" />
<div class="form-group">
<label asp-for="Password" class="col-md-2 control-label">Password</label>
<div class="col-md-10">
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword" class="col-md-2 control-label">Confirm Password</label>
<div class="col-md-10">
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="alert alert-success">
<p>Success!</p>
</div>
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System;

namespace ClassifiedAds.Application.AuditLogEntries.DTOs
namespace ClassifiedAds.Application.AuditLogEntries.DTOs;

public class AuditLogEntryDTO
{
public class AuditLogEntryDTO
{
public Guid Id { get; set; }
public Guid Id { get; set; }

public Guid UserId { get; set; }
public Guid UserId { get; set; }

public string UserName { get; set; }
public string UserName { get; set; }

public string Action { get; set; }
public string Action { get; set; }

public string ObjectId { get; set; }
public string ObjectId { get; set; }

public string Log { get; set; }
public string Log { get; set; }

public DateTimeOffset CreatedDateTime { get; set; }
}
public DateTimeOffset CreatedDateTime { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,40 @@
using System.Threading;
using System.Threading.Tasks;

namespace ClassifiedAds.Application.AuditLogEntries.Queries
namespace ClassifiedAds.Application.AuditLogEntries.Queries;

public class GetAuditEntriesQuery : AuditLogEntryQueryOptions, IQuery<List<AuditLogEntryDTO>>
{
}

internal class GetAuditEntriesQueryHandler : IQueryHandler<GetAuditEntriesQuery, List<AuditLogEntryDTO>>
{
public class GetAuditEntriesQuery : AuditLogEntryQueryOptions, IQuery<List<AuditLogEntryDTO>>
private readonly IAuditLogEntryRepository _auditLogEntryRepository;
private readonly IUserRepository _userRepository;

public GetAuditEntriesQueryHandler(IAuditLogEntryRepository auditLogEntryRepository, IUserRepository userRepository)
{
_auditLogEntryRepository = auditLogEntryRepository;
_userRepository = userRepository;
}

internal class GetAuditEntriesQueryHandler : IQueryHandler<GetAuditEntriesQuery, List<AuditLogEntryDTO>>
public async Task<List<AuditLogEntryDTO>> HandleAsync(GetAuditEntriesQuery query, CancellationToken cancellationToken = default)
{
private readonly IAuditLogEntryRepository _auditLogEntryRepository;
private readonly IUserRepository _userRepository;

public GetAuditEntriesQueryHandler(IAuditLogEntryRepository auditLogEntryRepository, IUserRepository userRepository)
{
_auditLogEntryRepository = auditLogEntryRepository;
_userRepository = userRepository;
}

public async Task<List<AuditLogEntryDTO>> HandleAsync(GetAuditEntriesQuery query, CancellationToken cancellationToken = default)
{
var auditLogs = _auditLogEntryRepository.Get(query);
var users = _userRepository.GetQueryableSet();
var auditLogs = _auditLogEntryRepository.Get(query);
var users = _userRepository.GetQueryableSet();

var rs = auditLogs.Join(users, x => x.UserId, y => y.Id,
(x, y) => new AuditLogEntryDTO
{
Id = x.Id,
UserId = x.UserId,
Action = x.Action,
ObjectId = x.ObjectId,
Log = x.Log,
CreatedDateTime = x.CreatedDateTime,
UserName = y.UserName,
});
var rs = auditLogs.Join(users, x => x.UserId, y => y.Id,
(x, y) => new AuditLogEntryDTO
{
Id = x.Id,
UserId = x.UserId,
Action = x.Action,
ObjectId = x.ObjectId,
Log = x.Log,
CreatedDateTime = x.CreatedDateTime,
UserName = y.UserName,
});

return await _userRepository.ToListAsync(rs.OrderByDescending(x => x.CreatedDateTime));
}
return await _userRepository.ToListAsync(rs.OrderByDescending(x => x.CreatedDateTime));
}
}
Loading

0 comments on commit 1b2eaae

Please sign in to comment.