简体   繁体   English

如何管理用户声明?

[英]How to manage user claims?

I'm currently designing a system using asp.net core and I'd like to implement claims based authorization, but one particular part is confusing me. 我目前正在设计一个使用asp.net核心的系统,我想实现基于声明的授权,但是一个特殊的部分让我感到困惑。

When a claim is made the claim will include the type and the value and optionally an issuer. 在提出索赔时,索赔将包括类型和价值以及可选的发行人。 In a handler this claim and the issuer may be checked before access is confirmed. 在处理程序中,可以在确认访问之前检查该声明和发行者。

However this issuer is not stored in the Identity db, so how does the handler then check the issuer? 但是,此发行者不存储在Identity db中,那么处理程序如何检查发行者?

Am I misunderstanding how this all works? 我误解了这一切是如何运作的吗? My understanding was that a user makes a claim of some type, that their claim is of a certain value and the issuer is the validator of the claim type actually having that value for that user. 我的理解是,用户提出某种类型的声明,他们的声明属于某个值,而发行者是声明类型的验证者,实际上具有该用户的该值。

The handler will check the value and may check the issuer but it can't when the db does not store it. 处理程序将检查值并可能检查发布者,但是当db不存储它时它不能。 I don't understand the point of the issuer then. 我不明白发行人的意思。

I'd like the user to have a collection of claims, including who/what verifies those claims and for the application to at any time be able to verify those claims. 我希望用户拥有一系列声明,包括谁/什么验证这些声明以及应用程序,以便随时验证这些声明。

Please help me understand. 请帮我理解。

I have tested this as so: 我已经测试了这个:

  1. Using a asp.net core app with Identity. 使用带有Identity的asp.net核心应用程序。
  2. Register a user. 注册用户。
  3. Add a claim to a user that includes a type, a value and an issuer. 向包含类型,值和颁发者的用户添加声明。 (for example, EmployeeNumber, 312, Microsoft. (例如,EmployeeNumber,312,Microsoft。
  4. Add an [Authorize(Policy="MicrosoftEmployeesOnly")] on a controller/action to restrict access. 在控制器/操作上添加[Authorize(Policy =“MicrosoftEmployeesOnly”)]以限制访问。
  5. Add the policy into services in StartUp.cs with a requirement. 使用要求将策略添加到StartUp.cs中的服务中。
  6. Add requirement code that has a handler that checks the user has a claim of type EmployeeNumber, has a value and it is issued by Microsoft. 添加需求代码,该代码具有检查用户具有类型EmployeeNumber的声明的处理程序,具有值并且由Microsoft发布。
  7. Login and the users claims will have been loaded in from the db into the identity. 登录和用户声明将从db加载到标识中。
  8. The handler will fail to validate the user because the issuer (Microsoft) has been lost and now just says Local Authority. 处理程序将无法验证用户,因为发行者(Microsoft)已丢失,现在只是说Local Authority。

The only thing I can think of here, is once the claim is added in to the db, it is considered validated by Microsoft and now held by the app (Local Authority) on behalf of Microsoft. 我唯一可以想到的是,一旦声明被添加到数据库中,它就被认为是微软验证的,现在由代表微软的应用程序(Local Authority)持有。

If that's true then: 如果那是真的那么:

  1. Why check the issuer at all in any handler? 为什么要在任何处理程序中检查发行者?
  2. How do you revoke a claim? 你如何撤销索赔?

I would prefer to be able to optionally go to that issuer and check the claim whenever I want, meaning the issuer could revoke/invalidate the claim. 我希望可以选择去那个发行人并随时检查索赔,这意味着发行人可以撤销/使索赔无效。 The employee makes the claim they have an employee number at Microsoft and initially Microsoft validate that. 该员工声称他们在Microsoft拥有员工编号,并且最初Microsoft会对此进行验证。 Some time later, Microsoft kick the employee out and on their system remove him. 一段时间后,微软将员工踢出去,他们的系统将他删除。 The app should be able to check with Microsoft each time the user logs in to see if the claim is valid. 每次用户登录时,应用程序都应该能够与Microsoft核实,以查看声明是否有效。 In this case it would not be valid any more. 在这种情况下,它将不再有效。

Am I going slightly mad? 我有点生气吗?

Posting this here as you linked to this question from my blog , and it may be useful to someone 当你从我的博客链接到这个问题时这里发布这个,这可能对某人有用

I think you have misunderstood slightly about the nature of a claim, which I can understand given the terminology. 我认为你对于索赔的性质略有误解,我可以理解术语。 You seem to be taking 'Claim' as meaning the user is 'professing' that they have a certain attribute, and you want to check that this is true. 您似乎将“声明”视为用户“自称”他们具有某种属性,并且您想要检查这是否属实。

That is not the way claims work here. 这不是索赔在这里工作的方式。 Claims are essentially 'attributes' of the user. 声明基本上是用户的“属性”。 In the old way of working with roles, a user would belong to a certain number of roles. 在使用角色的旧方式中,用户将属于特定数量的角色。 These are just 'attributes' the user has now, so are more generic. 这些只是用户现在拥有的“属性”,因此更通用。 A user may have a number of claims corresponding to the roles they are in. 用户可以具有与他们所处的角色相对应的多个声明。

The identity of the user is checked during authentication, and at that point you assign the set of Claims that the user has to the ClaimsIdentity object. 在身份验证期间检查用户的身份,此时您将用户拥有的Claims集分配给ClaimsIdentity对象。 This is the point you fetch the claims from the database, and make sure they only get the ones they should have. 这是您从数据库中获取声明的要点,并确保它们只获得它们应具有的声明。 If you need to have someone verifying claims, then you would need to have that whole process happening outside of this. 如果您需要某人验证声明,那么您需要在此之外进行整个过程。 Only the claims which have been confirmed should be added to the ClaimsIdentity . 只有已经确认的索赔才应添加到ClaimsIdentity

Now, there is an event you can handle on the CookieAuthenticationMiddleware to validate a security ticket when it is loaded on subsequent requests called ValidatePrincipal, but I'm not sure if this is actually what you need. 现在,您可以在CookieAuthenticationMiddleware上处理一个事件,以便在后续名为ValidatePrincipal的请求中加载安全票证时对其进行验证,但我不确定这实际上是否是您所需要的。

And your subsequent response: 以及随后的回复:

Thank you for your response. 感谢您的答复。 I understand now that these claims are effectively verified claims once they get into the db. 我现在明白,这些声明一旦进入数据库就会得到有效验证。 I guess they could be removed from the db as a way of revoking the claim. 我猜他们可以从数据库中删除,作为撤销声明的一种方式。

However, I think, as you suggest, the best way is to have this system outside and it just provides claims as and when required. 但是,我认为,正如您所建议的那样,最好的方法是将此系统放在外面,它只是在需要时提供索赔。 The design is that the application will have accounts for different types of entity and accounts will be able to make claims, for example that "I am a parent". 设计是应用程序将拥有不同类型的实体的帐户,并且帐户将能够提出索赔,例如“我是父母”。 The parent would seek an authorizing account to validate this. 家长会寻求授权帐户来验证这一点。 This might require the authorizing account holder to actually see some real documentation before validating. 这可能要求授权帐户持有人在验证之前实际查看一些真实的文档。 Other claims, could change. 其他索赔可能会改变。 For example a parent with Parental Responsibility would need a bit more verification, but may also lose that Parental Responsibility in the real world and so a facility for revoking the claim needs to be available. 例如,具有父母责任的父母需要更多的验证,但也可能在现实世界中失去父母的责任,因此需要提供撤销声明的工具。

So, I think the design should be to use the claims system with the Authorize attribute following your excellent articles, but have a separate system that allows for validation and revoking that feeds that claims system. 因此,我认为设计应该是在优秀文章之后使用具有Authorize属性的声明系统,但是有一个单独的系统允许验证和撤消声明系统的Feed。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM