如何处理用户权限 .net core mvc [英] How to work on user permission .net core mvc
本文介绍了如何处理用户权限 .net core mvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做人力资源员工模块,我卡在用户权限授权中,不知道怎么做.我附上了一个示例图像.有人可以帮助构建类似的东西以获得用户许可吗?
I am working on HR employee module and I am stuck in user permission authorization and don't know how do do it. I have attached an example image. Can someone help to build something like that for user permission?
推荐答案
Claims Register
Claims Register
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model, string
returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email =
model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await _userManager.AddClaimAsync(user, new
System.Security.Claims.Claim(ClaimTypes.Role, "Admin"));
await _userManager.AddClaimAsync(user, new
System.Security.Claims.Claim(ClaimTypes.Name, "Developer"));
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
// Send an email with this link
//var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
//var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
//await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
// $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, "User created a new account with password.");
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
你可以从这个开始..
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer
(Configuration.GetConnectionString("DefaultConnection")));
// Adjustment to params from the default settings
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext, int>()
.AddDefaultTokenProviders();
services.AddMvc();
#region Configure all Claims Policies
services.AddAuthorization(options =>
{
//options.AddPolicy("Administrators", policy =>
policy.RequireRole("Admin"));
options.AddPolicy("Administrators", policy =>
policy.RequireClaim(ClaimTypes.Role, "Admin")); // This works the same as
//the above code
options.AddPolicy("Name", policy =>
policy.RequireClaim(ClaimTypes.Name, "Bhail"));
});
#endregion
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
控制器
public class HomeController : Controller {
public IActionResult Index() {
return View(); }
[Authorize(Policy = "Administrators")]
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View();
}
[Authorize(Policy = "Name")]
public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";
return View();
}
public IActionResult Error()
{
return View();
}
}
登录
public async Task<ActionResult> Login(LoginViewModel model,string returnUrl)
{
var user = UserManager.Find(model.Email, model.Password);
if(user!=null)
{
var ident = UserManager.CreateIdentity(user,
DefaultAuthenticationTypes.ApplicationCookie);
ident.AddClaims(new[] {
new Claim("MyClaimName","MyClaimValue"),
new Claim("YetAnotherClaim","YetAnotherValue"),
});
AuthenticationManager.SignIn(
new AuthenticationProperties() { IsPersistent = true },
ident);
return RedirectToLocal(returnUrl);
}
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
这篇关于如何处理用户权限 .net core mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文