How to restrict web service data contract properties based on user role

家住魔仙堡 提交于 2019-12-23 04:13:10

问题


I have the following web service:

[DataContract]
public class Project
{
    public long Id { get; set; }
    public string Name { get; set; }
}

[OperationContract]
public Project GetProject(long Id);

Now I want to add a SecretData property that should only be exposed to certain users. I've come up with several ideas, but none of them sit quite well with me:

  1. Add a nullable SecretData property to Project. If the user doesn't have permission to view it, set it to null. This seems like the simplest approach, but how would a consumer tell the difference between a "no permission" null and a legitimate null value?
  2. Solution 1, but also add a boolean CanViewSecretData property. This addresses the legitimate null problem, but seems cumbersome.
  3. Provide a separate operation SecretDataType GetSecretData(long projectId) to retrieve the secret data, and return an error if the user does not have permission to call it. This keeps the data contract clean, but I see us ending up with lots of separate operations that need to be called in order to construct a full object.

Is there a better approach out there?


回答1:


I've got no idea what framework you are using, but from a general web services perspective here is what I suggest. In the XSD for Project, add an optional SecretData element to Project. When the user is not permitted to read SecretData, do not include the element in the response. Otherwise, include the element and when SecretData is null set the xsi:nil attribute to true.



来源:https://stackoverflow.com/questions/9526476/how-to-restrict-web-service-data-contract-properties-based-on-user-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!