forked from linezero/NETCoreBBS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountController.cs
More file actions
158 lines (138 loc) · 5 KB
/
Copy pathAccountController.cs
File metadata and controls
158 lines (138 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Identity;
using NetCoreBBS.Entities;
using NetCoreBBS.ViewModels;
using Microsoft.Extensions.Logging;
using System.Security.Claims;
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
namespace NetCoreBBS.Controllers
{
public class AccountController : Controller
{
private readonly ILogger<AccountController> _logger;
public AccountController(
UserManager<User> userManager,
SignInManager<User> signInManager,
ILogger<AccountController> logger)
{
UserManager = userManager;
SignInManager = signInManager;
_logger = logger;
}
public UserManager<User> UserManager { get; }
public SignInManager<User> SignInManager { get; }
//
// GET: /Account/Login
public IActionResult Login(string returnUrl = null)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
if (!ModelState.IsValid)
{
return View(model);
}
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
_logger.LogInformation("Logged in {userName}.", model.UserName);
return RedirectToLocal(returnUrl);
}
else
{
_logger.LogWarning("Failed to log in {userName}.", model.UserName);
ModelState.AddModelError("", "用户名或密码错误");
return View(model);
}
}
//
// GET: /Account/Register
public IActionResult Register()
{
return View();
}
//
// POST: /Account/Register
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new User { UserName = model.UserName, Email = model.Email,CreateOn=DateTime.Now,LastTime=DateTime.Now };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
_logger.LogInformation("User {userName} was created.", model.Email);
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
await MessageServices.SendEmailAsync(model.Email, "Confirm your account",
"Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
if (string.Equals(user.UserName, "admin", StringComparison.OrdinalIgnoreCase))
{
await UserManager.AddClaimAsync(user, new Claim("Admin", "Allowed"));
}
return RedirectToAction("Login");
}
AddErrors(result);
}
return View(model);
}
//
// GET: /Account/ForgotPassword
[AllowAnonymous]
public ActionResult ForgotPassword()
{
return View();
}
//
// POST: /Account/LogOff
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> LogOff()
{
var userName = HttpContext.User.Identity.Name;
await SignInManager.SignOutAsync();
_logger.LogInformation("{userName} logged out.", userName);
return RedirectToAction("Index", "Home");
}
public ActionResult AccessDenied()
{
return RedirectToAction("Index", "Home");
}
#region 辅助方法
private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
_logger.LogWarning("Error in creating user: {error}", error.Description);
}
}
private Task<User> GetCurrentUserAsync()
{
return UserManager.GetUserAsync(HttpContext.User);
}
private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
#endregion
}
}