Authentication With ASP.NET Core Identity
Authentication With ASP.NET Core Identity、
Preparing the Authentication Environment in our Project
The first thing, we are going to do is disable unauthorized users to access the Employees
action. To do that, we have to add the [Authorize]
attribute on top of that action:
[Authorize]
public async Task<IActionResult> Employees()
{
var employees = await _context.Employees.ToListAsync();
return View(employees);
}
Additionally, we have to add authentication middleware to the ASP.NET Core’s pipeline right above the app.UseAuthorization()
expression:
app.UseAuthentication();
If we run our application now and click on the Employees
link, we are going to get a 404 not found response:
We get this because, by default, ASP.NET Core Identity tries to redirect an unauthorized user to the /Account/Login
action, which doesn’t exist at the moment. Additionally, you can see a ReturnUrl
query string that provides a path to the required action before the user was redirected to the Login page. We are going to deal with it later in this post.
Now, we are going to do a couple of things to fix this 404 error.
自己控制登录
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(UserLoginModel userModel)
{
if(!ModelState.IsValid)
{
return View(userModel);
}
var user = await _userManager.FindByEmailAsync(userModel.Email);
if(user != null &&
await _userManager.CheckPasswordAsync(user, userModel.Password))
{
var identity = new ClaimsIdentity(IdentityConstants.ApplicationScheme);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));
identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme,
new ClaimsPrincipal(identity));
return RedirectToAction(nameof(HomeController.Index), "Home");
}
else
{
ModelState.AddModelError("", "Invalid UserName or Password");
return View();
}
}
登录成功之后,cookie list里面有.AspNetCore.Identity.Application这个cookie
Automating Authentication Process
If you want to take complete control over the authentication logic, the approach we have used is a great choice. But, we can speed up the process by using the SignInManger<TUser>
class. This class provides the API for user sign in with a lot of helper methods.
Conclusion
So, to sum it up, we have learned:
- How to execute the authentication process
- How to use different UserManager helper methods that help in a process
- The way to implement ReturnUrl logic
- And how to add additional claims to the user
In the next article, we are going to talk about Reset Password (Forgot Password) functionality with ASP.NET Core Identity.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-07-13 net use映射网盘和本地磁盘 How to Map Network Drives From the Command Prompt in Windows
2021-07-13 What is difference between “git checkout -f” and “git reset --hard HEAD”?
2021-07-13 What are the differences between .gitignore and .gitkeep?
2020-07-13 Comparison Review: Microsoft SSDT vs Redgate SQL Source Control
2020-07-13 Azure DevOps Azure Repos Git How-to Guides Use SSH key authentication
2019-07-13 de4dot 反混淆
2019-07-13 GreyMagic