首页 文章

ASP .NET中没有表单身份验证的基于角色的安全性

提问于
浏览
0

我想利用:

Page.User.IsInRole("CustomRole");
        Page.User.Identity.IsAuthenticated

在Page方法中工作时,以及web.config中的 authorization 部分:

<authorization>
    <allow roles="Administrators, Supervisors" />
    <deny users="*" />
</authorization>

并且还对类和方法级别应用规则:

[PrincipalPermission(SecurityAction.Demand, Role = "Administrators")]

在我的应用程序中,我使用...自定义机制进行身份验证,在... http标头中提供用户身份 . 我获得用户PIN号码(某种ID)角色 . 但这是一个侧面阴谋 . 没关系 .

我真正想要实现的是利用ASP .NET构建授权功能,但拥有自定义身份验证机制 . 我想我必须实现 IPrincipalIIdentity ,是吗?我在网上看到了很多样本,但是所有这些样本都包含指定提供者的web.config配置,以及类似于 FormsAuthentication 的类,我想我不喜欢它 .

所以:

  • 实现它的最简单方法是什么?

  • GenericPrincipal / IPrincipal有什么区别?

  • 如何获取/创建IIdentity对象?我看到样品:

var id = new FormsIdentity(authTicket);

但我没有使用FormsAuthentication .

谢谢

2 回答

  • 0

    简而言之,您必须实现自己的身份验证模块 .

    身份验证模块只是一个ASP.NET模块,但具有特殊用途 . 其 AuthenticateRequest 方法应使用 IPrincipal 实例填充 HttpContext.Current.User 属性 .

    回答你的其他问题: IPrincipal 只是一个界面,而 GenericPrincipal 是它的一个实现 . 您可以使用它,顾名思义它只是一个通用的实现,这意味着它应该适合您 . 由于 IPrincipal 只是 IIdentity 加角色,您可能还需要 GenericIdentity .

    其他实现,如 RolePrincipal FormsIdentity 是为特定目的而设计的,例如,这两个实现由表单身份验证模块使用 .

    有一些很好的例子,只是谷歌的“自定义身份验证模块” .

  • 0

    在您(创建/实现自己的)之前,您是否已尝试/考虑将表单身份验证调整为现有的身份验证方案?

    我认为你是"almost there"(使用所有内置的ASP.net auth / membership / profiles / roles),并且将现有的auth方案_____4328_更简单/更简单到表单身份验证 .

    snippet of code应该让您了解表单身份验证的灵活性:

    if ((UserEmail.Text == "jchen@contoso.com") && (UserPass.Text == "37Yj*99Ps"))
    {
          FormsAuthentication.RedirectFromLoginPage 
             (UserEmail.Text, Persist.Checked);
    }
    else
    { ... }
    

    所以,它适用于硬编码的"auth scheme"(不是你应该这样,但是让你了解可能性),甚至列表in web.config - 再次,只是一个样本:

    <authentication mode="Forms">
      <forms name=".FUBAR">
        <credentials passwordFormat="MD5">
          <user name="foo" password="b7ab5072e8fba7bed20384cc42e96193"/>
          <user name="bar" password="1c42e49a360aa7cc337a268a1446a062"/>
          <user name="john" password="5f4dcc3b5aa765d61d8327deb882cf99"/>
          <user name="jane" password="7c6a180b36896a0a8c02787eeafb0e4c"/>
        </credentials>
      </forms>
    </authentication>
    

    只是一个想法 - ......

相关问题