1

我正在与Azure ACS 实验室合作,他们使用FederatedServiceCredentials对 Active Federation 的用户进行身份验证。现在我想从 WCF 服务中访问用户的声明。

根据这篇文章,声明由请求线程访问......任何人都可以解释或证明这意味着什么?

4

1 回答 1

2

请求线程是在服务器上执行服务 API 的线程。从该线程(也就是在您的服务 api 中),您可以访问 Thread.CurrentPrincipal.Identity。这将是一个 ClaimsIdentity,其中包含 STS 授予您的声明。例如:

 class MyService:IService
 {
   // code running on wcf server
   bool AdminOnlyApi()
   {    
     var identity = Thread.CurrentPrincipal.Identity as ClaimsIdentity;

     // fail all non admin callers.
     if (!identity.Claims.Exists(c=>c.ClaimType=="role" && c.Value=="Admin"))
     {
        throw new SecurityException("Access is denied.");
     }
     return True;
   }  
 }
于 2011-04-01T06:22:57.613 回答