c# - 如何在 MVC5 中手动检查 url 授权?

标签 c# asp.net-mvc iis asp.net-mvc-5 authorization

IIS 管理器

要限制对网络应用的访问,管理员可以通过 IIS 管理器设置用户和组的 url 授权:

IIS Autohrization Rules

Web.config

IIS 管理器将授权规则存储在应用程序的 web.config 中:

<security>
  <authorization bypassLoginPages="true"> 
    <remove users="*" roles="" verbs="" />
    <add accessType="Allow" users="Testuser" />
    <add accessType="Deny" users="*" /> 
  </authorization>
</security>

bypassLoginPages设置为true时,所有用户都被授权访问登录页面。当用户未登录时,他将被自动重定向到登录页面:

<authentication mode="Forms">
  <forms [...] loginUrl="~/Auth/Login" [...] >
    [...]
  </forms>
</authentication>

MVC5 应用:

用户必须使用他的 Windows SamAccountName 和密码通过自定义登录页面登录。凭据将发送到 AuthControllerLogin 操作:

[AllowAnonymous]
public class AuthController : Controller
{
    public ActionResult Login
    {
        // validation of SamAccountName and Password against Active Directory here.

        [...]

        // We want to check the authorization here.

        // create authentication ticket
        FormsAuthenticationTicket lFormsAuthenticationTicket = new FormsAuthenticationTicket(1,
            SamAccountName,
            DateTime.Now,
            DateTime.Now.AddMinutes(AuthCookieTimeout),
            RememberMe,
            CustomData,
            FormsAuthentication.FormsCookiePath);

        // Encrypt the ticket.
        string lEncryptedTicket = FormsAuthentication.Encrypt(lFormsAuthenticationTicket);

        var lAuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, lEncryptedTicket);

        // Create the cookie.
        Response.Cookies.Add(lAuthCookie);

        [...]

        return RedirectToAction("Index", "Main"); // redirect to the main controller
    }
}

所有受限 Controller 都通过 [Authorize] 属性自动进行授权检查:

[Authorize]
public class MainController : Controller
{
    [...]
}

[Authorize(Users="User1,User2")] 之类的装饰不是解决方案,因为最终用户无法访问代码,最终用户应该可以配置对应用程序的访问。

当用户未被授权时,他将被重定向到登录页面。那很好用。但是我之前需要在 Login 操作中进行授权检查。所以我的问题:

如果登录用户有权重定向到 MainController,我如何在我的 AuthController 中手动验证?

最佳答案

Q: How can I manually validate in my AuthController if the logged in user is authorized to redirect to the MainController?

由于您使用的是 Authorize属性,您无需在操作中手动检查授权。这些是一些规则:

  • 限制对经过身份验证的用户的访问:[Authorize]
  • 限制某些特定用户的访问:[Authorize(Users="User1,User2")]
  • 限制对某些特定角色的访问:[Authorize(Roles="Administrators,PowerUsers")]

由于您使用 Authorize 属性装饰了 MainController,这意味着没有人可以在没有登录的情况下访问它的操作。 因此,在 Logon 操作中,您无需检查用户是否有权重定向到主 Controller 。这里没有任何安全漏洞,您在使用 RedirectToAction("Index", "Main") 时无需担心授权问题。

Q: A definition in the the Authorize attribute would not solve the problem. How can Administrators restrict users and groups when they buy the software? Thy have no access to the code.

角色是为满足这样的要求而创建的。您应该在 MainController 之上使用 [Authorize(Roles="Role1")] 然后 Role1 的每个用户都可以访问主 Controller 的操作。它可以简单地在应用程序的用户和角色管理中完成。所以:

  1. 在开发时,用静态角色装饰 Controller 和 Action
  2. 在运行时,您可以使用您的应用程序管理用户角色。

注意

在大多数应用程序中,角色是静态的,您可以指定哪个角色可以访问哪个操作。在这种情况下,当前的 Authorize 属性就足以进行授权。只需在运行时将用户添加到角色。 Identity Samples包含执行此操作所需的模型、 View 和 Controller 。

如果您想在运行时创建新角色或在运行时更改角色的权限,您需要创建一个新的 Authorize 属性,该属性从配置文件或数据库,并从配置文件或数据库中读取角色的权限并决定授权。

关于c# - 如何在 MVC5 中手动检查 url 授权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40672170/

相关文章:

javascript - 如何在初始 View 模型加载为空时初始化 Knockout View 模型

asp.net - Azure Web应用程序,自定义状态代码描述不起作用

c# - 为什么 foreach 会跳过接口(interface)类型的编译时类型检查?

javascript - Jquery 在 cshtml 页面中不工作

c# - Unity3D UI,计算拖动项目的位置?

asp.net-mvc - ASPNET MVC 自定义路由失败,返回 "No type was found that matches the controller named ' Home'。”

iis - IIS 在哪里存储有关每个绑定(bind)的 SSL 证书协议(protocol)映射的信息?

iis - 具有不同内容的两台服务器可以共享一个子域吗?

c# - 在 DisplayNameAttribute 派生类中获取属性和类名

c# - 这比 Null Coalesce 运算符好在哪里?