gpt4 book ai didi

c# - 如何正确使用【授权】

转载 作者:行者123 更新时间:2023-11-30 21:46:46 29 4
gpt4 key购买 nike

我有两组用户,一组正在招聘,一组正在招聘。

我想限制每个用户组对某些页面的访问,但是当我在 Controller 中使用 [Authorize] 时,它允许访问任何已登录的用户而不区分他们来自哪个组?

   [HttpPost]
public ActionResult Signin(string username, string password)
{
var mgr = new Cleaners.Models.UserManager(@"Data Source=.\sqlexpress;Initial Catalog='Cleaning Lady';Integrated Security=True");
var user = mgr.GetUser(username, password);
if (user == null)
{
return View(new UserViewModel { Name = username });
}

FormsAuthentication.SetAuthCookie(user.UserName, true);
UserViewModel.IsAuthenticated = User.Identity.IsAuthenticated;
return RedirectToAction("Private");
}

[Authorize]
public ActionResult Private()
{
return View();

}

有什么方法可以限制通过此 Controller 验证的用户对“私有(private)”的访问吗?

最佳答案

我举一个简单的例子:

    [Authorize(Roles="Contractor")]
public ActionResult Private()
{
return View();

}

这将检查当前用户/身份是否具有名为 Contractor 的角色。

我建议您阅读 this article了解它的基础知识。

关于c# - 如何正确使用【授权】,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39022454/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com