Template
mirror of
https://github.com/egmont11/ASP.Net-Core-MVC-Template.git
synced 2026-07-24 07:09:56 +02:00
Added some GDPR things (data export, deletion, edit)
This commit is contained in:
@@ -44,9 +44,43 @@ public class AuthController : Controller
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> Profile()
|
||||
{
|
||||
var userResult = await _authService.FindUserByEmailOrUsernameAsync(User.Identity?.Name ?? "");
|
||||
if (!userResult.Success || userResult.Data == null) return RedirectToAction("Login");
|
||||
|
||||
var model = new ProfileViewModel
|
||||
{
|
||||
UserName = userResult.Data.UserName,
|
||||
Email = userResult.Data.Email
|
||||
};
|
||||
return View(model);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region httpPost
|
||||
[HttpPost] [Authorize] [ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Profile(ProfileViewModel model)
|
||||
{
|
||||
if (!ModelState.IsValid) return View(model);
|
||||
|
||||
var userIdClaim = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier);
|
||||
if (userIdClaim == null || !int.TryParse(userIdClaim.Value, out int userId)) return RedirectToAction("Login");
|
||||
|
||||
var result = await _authService.UpdateProfileAsync(userId, model);
|
||||
if (!result.Success)
|
||||
{
|
||||
ModelState.AddModelError(string.Empty, result.Message);
|
||||
return View(model);
|
||||
}
|
||||
|
||||
TempData["SuccessMessage"] = "Profile updated successfully. Please log in again if you changed your username.";
|
||||
return RedirectToAction("Profile");
|
||||
}
|
||||
|
||||
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Login(AuthLoginViewModel model)
|
||||
{
|
||||
@@ -136,5 +170,21 @@ public class AuthController : Controller
|
||||
|
||||
return RedirectToAction("Register");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> DownloadData()
|
||||
{
|
||||
var userIdClaim = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier);
|
||||
if (userIdClaim == null || !int.TryParse(userIdClaim.Value, out int userId)) return RedirectToAction("Login");
|
||||
|
||||
var result = await _authService.GetUserDataAsync(userId);
|
||||
if (!result.Success || result.Data == null) return BadRequest(result.Message);
|
||||
|
||||
var json = System.Text.Json.JsonSerializer.Serialize(result.Data, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
|
||||
var bytes = System.Text.Encoding.UTF8.GetBytes(json);
|
||||
|
||||
return File(bytes, "application/json", $"user_data_{User.Identity?.Name}.json");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user