学习Net Core 2.0 做博客 identity登陆权限授权
2021-05-16 01:28
标签:ppc msi json update bsp www. from 用户权限 ade 定义类RolePermissionViewModel 保存角色权限 定义PermissionRequirement类,实现IAuthorizationRequirement 定义PermissionHandler类,继承AuthorizationHandler 添加一个用户权限初始化类RolePermissionInit 在Program中执行RolePermissionInit.Init 设置action权限名称特性类SetActionAttribute Startup类: ConfigureServices中注入相关配置 添加自定义授权支持,并添加使用Cookie的方式,配置登录页面和没有权限时的跳转页面。 在Configure中使用 使用: 登陆验证提交成功后: 退出: 控制器权限控制 默认使用action名称控制,可以使用[SetAction(ActionName ="save")] 表示当前action需要save保存的权限 参考大牛文章网址:http://www.cnblogs.com/axzxs2001/p/7482771.html 学习Net Core 2.0 做博客 identity登陆权限授权 标签:ppc msi json update bsp www. from 用户权限 ade 原文地址:http://www.cnblogs.com/wyzy/p/7749689.html public class RolePermissionViewModel
{
///
public class PermissionRequirement:IAuthorizationRequirement
{
///
public class PermissionHandler : Microsoft.AspNetCore.Authorization.AuthorizationHandler
public static class RolePermissionInit
{
public static void Init(IServiceProvider app)
{
PermissionHandler _permissionHandler = app.GetRequiredService
public class Program
{
public static void Main(string[] args)
{
// BuildWebHost(args).Run();
var host = BuildWebHost(args)
.Migrate();//初始化数据
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
RolePermissionInit.Init(services);
}
catch (Exception ex)
{
var logger = services.GetRequiredService
public class SetActionAttribute:Attribute
{
public string ActionName { get; set; }
}
services.AddAuthorization(option => {
option.AddPolicy("Permission", policy => policy.Requirements.Add(new PermissionRequirement("/admin/account/denied")));
})
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(option=> {
option.LoginPath = new PathString("/admin/account/index");
option.AccessDeniedPath = new PathString("/admin/account/denied");
});
services.AddSingleton
app.UseAuthentication();
[Microsoft.AspNetCore.Authorization.Authorize(Policy = "Permission")]
public class AdminBaseController : Controller
{
public override void OnActionExecuting(ActionExecutingContext context)
{
}
}
//获取用户所有角色
var roles = string.Join(",", SysUserRoleService.GetListJoin(s => s.Enable == true&&s.UserId==userinfo.Id, new string[] { "SysRole" }).Select(s => s.SysRole.Name).ToArray());
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Sid, model.LoginName));
identity.AddClaim(new Claim(ClaimTypes.Name, userinfo.LoginName));
identity.AddClaim(new Claim("RealName", userinfo.RealName));
identity.AddClaim(new Claim(ClaimTypes.Role, roles));
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
public async Task
[Area("admin")]
public class BlogManagerController : AdminBaseController
{
IBlogCategoryService BlogCategoryService { get; set; }
IBlogArticleService BlogArticleService { get; set; }
public BlogManagerController(IBlogCategoryService blogCategoryService, IBlogArticleService blogArticleService)
{
BlogCategoryService = blogCategoryService;
BlogArticleService = blogArticleService;
}
public IActionResult Index()
{
return View();
}
#region 添加
[HttpGet]
public IActionResult Add()
{
var list = BlogCategoryService.GetList(c => c.Enable == true && c.CategoryType == Blog.Models.Enum.BlogCategoryType.General && c.Pid == 0);
var categoryList = (from c in list
select new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
}).ToList();
ViewBag.categoryList = categoryList;
return PartialView();
}
[HttpPost]
[SetAction(ActionName ="save")]
public IActionResult Add(BlogArticle entity)
{
Response res = null;
if (ModelState.IsValid)
{
Request.Form.TryGetValue("childCategory", out StringValues categoryId);
if (!string.IsNullOrEmpty(categoryId.ToString()))
{
entity.CategoryId = Convert.ToInt32(categoryId.ToString());
}
entity.Stick = false;
entity.Recommend = false;
entity.Submitter = HttpContext.User.Claims.SingleOrDefault(u => u.Type == "RealName").Value;
entity.Traffic = 0;
entity.CommentNum = 0;
entity.CreateTime = DateTime.Now;
entity.UpdateTime = DateTime.Now;
entity.CategoryName = BlogCategoryService.Find(c => c.Id == entity.CategoryId).Name;
res = BlogArticleService.Add(entity);
}
else
{
res = new Common.Response() { Code = ResponseCode.Success, Message = Utils.ModelStateMessage(ModelState) };
}
return Json(res);
}
#endregion
}
上一篇:php7 安装xhprof
文章标题:学习Net Core 2.0 做博客 identity登陆权限授权
文章链接:http://soscw.com/index.php/essay/86037.html