【问题标题】:How to implement claims-based authentication using Identity 2.0 in Asp.net MVC如何在 Asp.net MVC 中使用 Identity 2.0 实现基于声明的身份验证
【发布时间】:2017-09-30 07:15:33
【问题描述】:

我正在开发一个使用身份 2 对用户进行身份验证和授权的 Asp.net Mvc 应用程序,但似乎我需要比基于角色更多的功能,所以我想更改我的方法并使用基于声明的方法来创建应用程序。

更新:考虑我想为特定用户设置访问权限以访问特定操作。

但问题是没有什么可学的,我的意思是我知道声明是什么,但我不知道如何实现它并创建用户和事物。

我想知道为什么没有什么可以学习如何实现基于声明的!这就是我问这个问题的原因。

我需要一些准备好的项目或分步教程。 有什么可以教如何处理索赔的吗?

【问题讨论】:

  • 最好的办法可能是将自定义访问信息存储在资源数据库中。换一种说法。您无需将其放入声明或身份框架中。您已经确定了用户,并且该用户可以链接到您的资源(例如添加带有 UserId 的声明)。无需通过网络发送此详细信息。
  • 是的,我认为这是一种更好的方法,但我意识到,也许使用声明是执行我所说的标准方法。顺便说一句,我做了类似于你的想法@RuardvanElburg
  • 一般来说,Dominick Baier (IdentityServer4) 的博客读起来很有趣。包含大量信息和想法:leastprivilege.com/2016/12/16/identity-vs-permissions

标签: c# asp.net asp.net-mvc identity claims-based-identity


【解决方案1】:

请查看 Asp.Net Core 中的策略。

在Policiy 中,您可以使用Claims + Roles + 任何您想要的。所有这些都内置在 asp.net 中。

这里是官方参考:

https://docs.microsoft.com/en-us/aspnet/core/security/authorization/claims https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies

如果您不使用 .Net Core,您将需要像此授权过滤器这样的自定义实现。

public class ClaimsAuthorizeAttribute : AuthorizeAttribute
{
    private string claimType;
    private string claimValue;
    public ClaimsAuthorizeAttribute(string type, string value)
    {
        this.claimType = type;
        this.claimValue = value;
    }
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        var user = filterContext.HttpContext.User as ClaimsPrincipal;
        if (user != null && user.HasClaim(claimType, claimValue))
        {
            base.OnAuthorization(filterContext);
        }
        else
        {
            base.HandleUnauthorizedRequest(filterContext);
        }
    }
}

MVC5 Claims version of the Authorize attribute提取的代码

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 2019-01-01
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-30
相关资源
最近更新 更多