2016-01-04 28 views
0

我目前正在编写一个角度应用程序,首先进行身份验证,认为纹理identityserver3。 这工作正常,我收到持票人令牌没有任何问题。 当我在对我的API的调用中使用我的令牌时,我已通过身份验证。我可以看到我的用户名,但已经失去了我的声明(用户名,角色......)。 对于使用我的令牌传输我的声明或从身份服务器获取角色,我需要做些什么?身份服务器声明asp.net API

回答

1

您可以通过将声明添加到您的API的范围,告诉Identity Server将特定声明包含在访问令牌中。

实施例:

var apiScope = new Scope { 
    Name = "myApi", 
    DisplayName = "My API", 
    Type = ScopeType.Resource, 
    Claims = new List<ScopeClaim> { 
     new ScopeClaim("myClaimType") 
    } 
}; 

还可以使用的ScopeClaimAlwaysIncludeInIdToken属性包括在身份令牌以及访问令牌的权利要求。

有关更多信息,请参阅https://identityserver.github.io/Documentation/docsv2/configuration/scopesAndClaims.html

0

我们正在使用MS Web API 2和Thinktecture Identity Server v3做类似的事情。

要验证用户的声明,我们创建了一个身份验证过滤器,然后直接调用身份服务器以获取用户的声明。不记名令牌只授予认证,并由API分别获取权利要求。

protected override bool IsAuthorized(HttpActionContext actionContext) 
    { 
     string identityServerUrl = WebConfigurationManager.AppSettings.Get("IdentityServerUrl") + "/connect/userinfo"; 

     using (var httpClient = new HttpClient()) 
     { 
     httpClient.DefaultRequestHeaders.Authorization = actionContext.Request.Headers.Authorization; 

     var response = httpClient.GetAsync(identityServerUrl).Result; 
     if (response.IsSuccessStatusCode) 
     { 
      string responseString = response.Content.ReadAsStringAsync().Result; 
       Dictionary<string, string> claims = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseString.ToLower()); 
      ... Do stuff with your claims here ... 
     } 
    } 
}