随笔分类 -  C# / Identity4

摘要:.net 默认新建Api项目不需要额外从Nuget添加Microsoft.AspNetCore.Authentication.JwtBearer appsettings.json { "Logging": { "LogLevel": { "Default": "Information", "Micr 阅读全文
posted @ 2020-12-07 15:32 liessay 阅读(324) 评论(0) 推荐(0) 编辑
摘要:详情访问官方文档 例如,以下代码将访问权限限制为属于角色成员的用户的任何操作 AdministrationController Administrator : [Authorize(Roles = "Administrator")] public class AdministrationContro 阅读全文
posted @ 2020-06-30 14:31 liessay 阅读(495) 评论(0) 推荐(0) 编辑
摘要:详情访问官方文档 以下代码将访问权限限制为任何经过身份验证的用户,这里为控制器级 [Authorize] public class AccountController : Controller { public ActionResult Login() { } public ActionResult 阅读全文
posted @ 2020-06-30 14:28 liessay 阅读(454) 评论(0) 推荐(0) 编辑
摘要:因需要在用户列表中点详情按钮来到当前页,所以需要展示分组详情,并展示当前所属角色组的用户 public async Task<ActionResult> Details(string id) { //查找是否存在角色组 var role = await _roleManager.FindByIdAs 阅读全文
posted @ 2020-06-30 14:04 liessay 阅读(925) 评论(0) 推荐(0) 编辑
摘要:角色删除方法 [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Delete(string id) { var role = await _roleManager.FindByIdAsync(id); if ( 阅读全文
posted @ 2020-06-30 10:19 liessay 阅读(627) 评论(0) 推荐(0) 编辑
摘要:因只有角色名能修改故继续使用创建角色的视图模型 using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace Shop.ViewModel { public class CreateEditRo 阅读全文
posted @ 2020-06-30 10:05 liessay 阅读(470) 评论(0) 推荐(0) 编辑
摘要:首先创建视图模型 using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace Shop.ViewModel { public class CreateEditRoleViewModel { [ 阅读全文
posted @ 2020-06-30 10:02 liessay 阅读(499) 评论(0) 推荐(0) 编辑
摘要:需要将目前所有角色名显示出来,方法同用户管理 一、创建Index acction public async Task<ActionResult> Index() { var roles = await _roleManager.Roles.ToListAsync(); return View(rol 阅读全文
posted @ 2020-06-30 09:47 liessay 阅读(561) 评论(0) 推荐(0) 编辑
摘要:因角色管理需要有用户才能进行(需要将用户从角色中添加,删除)故角色管理代码依托用户管理 只需在Startup服务中添加角色管理即可完成 public void ConfigureServices(IServiceCollection services) { services.AddControlle 阅读全文
posted @ 2020-06-30 09:22 liessay 阅读(645) 评论(0) 推荐(0) 编辑
摘要:在实际使用时会发现很多字段在IdentityUser中并不存在,比如增加生日,地址等字段,可在模型类中实现自己的模型并继承自IdentityUser,需要修改的代码为以下类 一、新增模型 using System; using Microsoft.AspNetCore.Identity; names 阅读全文
posted @ 2020-06-29 17:07 liessay 阅读(1030) 评论(0) 推荐(1) 编辑
摘要:目前用户管理的增删改查及登录功能已经全部实现,但存在一个问题,登录后要取消登录按钮显示退出按钮,未登录应该有注册按钮,现实现过程如下 一、Startup.cs中增加服务 app.UseAuthentication(); 二、修改用户管理主页 注入服务 @inject SignInManager<Id 阅读全文
posted @ 2020-06-29 16:05 liessay 阅读(813) 评论(0) 推荐(0) 编辑
摘要:一、建立LoginViewModel视图模型 using System.ComponentModel.DataAnnotations; namespace Shop.ViewModel { public class LoginViewModel { [Required] [Display(Name 阅读全文
posted @ 2020-06-29 15:11 liessay 阅读(906) 评论(0) 推荐(0) 编辑
摘要:修改用户不能修改Id及用户名所以创建视图模型时需要去除,新增用户跟修改用户基本视图一直,所以不再做演示 一、新建UpdateUserViewModel视图模型 using System.ComponentModel; using System.ComponentModel.DataAnnotatio 阅读全文
posted @ 2020-06-29 14:09 liessay 阅读(852) 评论(0) 推荐(1) 编辑
摘要:用户注册主要有2个方法,1、密码加密 2、用户注册 3、ASP.NET Core Identity 使用密码策略、锁定和 cookie 配置等设置的默认值。 可以在类中重写这些设置 Startup(官方详情点这里) 首先创建CreateUserViewModel视图模型 using System.C 阅读全文
posted @ 2020-06-29 13:45 liessay 阅读(874) 评论(0) 推荐(0) 编辑
摘要:在Controllers中新建AccountController,并在构造方法(函数)中注入SignInManager,UserManager UserManager 用户管理(注册,查找,修改,删除用户等) SignInManager 用户登录管理(登录,注销等) private readonly 阅读全文
posted @ 2020-06-29 13:36 liessay 阅读(1095) 评论(0) 推荐(0) 编辑
摘要:理论知识微软官方文档最完整,最详细,这里只一步步的介绍如何使用,地址:https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/?view=aspnetcore-3.1 一、新建Mvc项目 二、加入EF依赖 浏览输入en 阅读全文
posted @ 2020-06-29 13:10 liessay 阅读(1099) 评论(1) 推荐(1) 编辑