【问题标题】:ASP.NET MVC Assign Roles when using custom authentication使用自定义身份验证时 ASP.NET MVC 分配角色
【发布时间】:2011-05-19 16:34:17
【问题描述】:

我使用 MongoDB 来存储用户和密码(而不是 sql)。我有自己的机制来为用户分配角色。我想用角色来装饰我的控制器动作,如下所示:

[Authorize(Roles = "Administrators")]

我的登录代码如下所示:

if (mongo.AuthenticateUser(model.UserName, model.Password))
{
    FormsService.SignIn(model.UserName, model.RememberMe);
}

当用户首次登录时,我将用户角色“标记”到用户上的最简单方法是什么(数据库将为我提供角色名称,我只需在用户上下文中设置它们)。我想我正在寻找我的代码看起来像这样:

if (mongo.AuthenticateUser(model.UserName, model.Password))
{
    if ((mongo.IsAdmin)
    {
        // How do I grant the admin role???
        SomeSortOfUserContext.Add('Administrators');????????
    }
    FormsService.SignIn(model.UserName, model.RememberMe);
}

【问题讨论】:

    标签: asp.net-mvc security authentication authorization


    【解决方案1】:

    在 Global.asax 的 Application_AuthenticateRequest 中为用户主体添加角色。

    签出this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 2016-04-14
      • 2013-01-05
      相关资源
      最近更新 更多