Authorization in WCF
Using Impersonation
1. Open the application that you created using WCF Security blog.
2. Let's say the application requires that the user who invokes a method is authorized to make changes to a file or write to a folder. To do this, we have to enable authorization. In the previous exercise we used the user, user2 to invoke the methods in the service. Let's now check if the user2 is authorized to write to a folder.
3. Change the SubmitReview method in Service project as given below:
void IProductService.SubmitReview(ProductReview pr)
{
using (FileStream stream = new FileStream("lastMessage.xml", FileMode.Create, FileAccess.Write))
{
DataContractSerializer dcs = new DataContractSerializer(typeof(ProductReview));
dcs.WriteObject(stream, pr);
}
Reviews.Add(pr);
}
4. Run and test the application. It should work alright now. This is because host process's identity (current user's identity) is used to invoke the SubmitReview method. To use the identity provided by the client's clientCredentials and to authorize based on the windows access control list, do the following changes to the service implementation.
[OperationBehavior(Impersonation=ImpersonationOption.Required)]
void IProductService.SubmitReview(ProductReview pr)
5. Run and test the application again. Notice that this time it failed. Try providing access to User2 and execute the application again.
Using Role Based Authorization
6. Create a user group in your computer and add users to the group. Use the instructions here.
7. Do the following changes to the service implementation:
//[OperationBehavior(Impersonation=ImpersonationOption.Required)]
[PrincipalPermission(SecurityAction.Demand, Role="YourComputerName\\Customers")]
void IProductService.SubmitReview(ProductReview pr)
8. Run and test. First, let the client use identity of users who are not in the group. Then, test with users who are in the group. If the user identity used by the client is not in the group then it should throw an exception on the client end.
References and Links
Authentication and Authorization in WCF Services - Part 1 - https://msdn.microsoft.com/en-us/library/ff405740.aspx
Comments
Post a Comment