summaryrefslogtreecommitdiff
path: root/Controllers
diff options
context:
space:
mode:
Diffstat (limited to 'Controllers')
-rw-r--r--Controllers/AccountController.cs80
-rw-r--r--Controllers/BundleController.cs2
-rw-r--r--Controllers/FigureController.cs2
3 files changed, 84 insertions, 0 deletions
diff --git a/Controllers/AccountController.cs b/Controllers/AccountController.cs
new file mode 100644
index 0000000..432b671
--- /dev/null
+++ b/Controllers/AccountController.cs
@@ -0,0 +1,80 @@
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Identity;
+using Microsoft.AspNetCore.Mvc;
+using MyDarling.Models;
+
+namespace MyDarling.Controllers;
+
+public class AccountController : Controller
+{
+ public UserManager<IdentityUser> UserManager { get; set; }
+ private SignInManager<IdentityUser> SignInManager;
+
+ public AccountController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager)
+ {
+ UserManager = userManager;
+ SignInManager = signInManager;
+ }
+
+ public IActionResult List()
+ {
+ return View(UserManager.Users);
+ }
+
+ public IActionResult Create()
+ {
+ return View(new IdentityUser());
+ }
+
+ [HttpPost]
+ public async Task<IActionResult> Create([Bind] IdentityUser user, [Bind] string Password)
+ {
+ if (ModelState.IsValid)
+ {
+ IdentityResult result = await UserManager.CreateAsync(user, Password);
+ if (result.Succeeded)
+ {
+ return RedirectToAction(nameof(List));
+ }
+
+ foreach (IdentityError error in result.Errors)
+ {
+ ModelState.AddModelError("", error.Description);
+ }
+ }
+ return View();
+ }
+
+ public ViewResult Login(string returlUrl)
+ {
+ return View(new LoginModel { ReturnUrl = returlUrl });
+ }
+
+ [HttpPost]
+ public async Task<IActionResult> Login(LoginModel loginModel)
+ {
+ if (ModelState.IsValid)
+ {
+ IdentityUser user = await UserManager.FindByNameAsync(loginModel.Name);
+ if (user != null)
+ {
+ await SignInManager.SignOutAsync();
+ if ((await SignInManager.PasswordSignInAsync(user,
+ loginModel.Password, false, false))
+ .Succeeded)
+ {
+ return Redirect(loginModel?.ReturnUrl ?? "/Bundle");
+ }
+ }
+ ModelState.AddModelError("", "Invalid name or password");
+ }
+ return View(loginModel);
+ }
+
+ [Authorize]
+ public async Task<RedirectResult> Logout(string returlUrl = "/Account/Login")
+ {
+ await SignInManager.SignOutAsync();
+ return Redirect(returlUrl);
+ }
+} \ No newline at end of file
diff --git a/Controllers/BundleController.cs b/Controllers/BundleController.cs
index 9e5c2d9..b1d61fd 100644
--- a/Controllers/BundleController.cs
+++ b/Controllers/BundleController.cs
@@ -1,10 +1,12 @@
using System.Data;
+using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyDarling.Models;
namespace MyDarling.Controllers
{
+ [Authorize]
public class BundleController : Controller
{
private DataContext context;
diff --git a/Controllers/FigureController.cs b/Controllers/FigureController.cs
index 3329968..9f6cf0c 100644
--- a/Controllers/FigureController.cs
+++ b/Controllers/FigureController.cs
@@ -1,10 +1,12 @@
using System.Data;
+using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyDarling.Models;
namespace MyDarling.Controllers
{
+ [Authorize]
public class FigureController : Controller
{
private DataContext context;