我正在尝试使用OAuth身份验证和OAuth在我的webapp和webapi之间进行通信。我正在使用ADFS4,并相应地配置了应用程序组与服务器应用程序和Webapi。我试图收到userdetails,特别是webapi控制器的用户名。是否可以在传递给webapi的访问令牌中传递用户名详细信息。这是我在Webapp方面所做的:
在adfs身份验证之后的webapp控制器中,
authContext = new AuthenticationContext(Startup.authority, false);
ClientCredential credential = new ClientCredential(Startup.clientId, Startup.appKey);
string accessToken = null;
bool isAuthenticated = User.Identity.IsAuthenticated; //return true
string username = User.Identity.Name; // returns username
string userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Name).Value; // returns username
HttpClient httpClient = new HttpClient();
try
{
result = authContext.AcquireTokenAsync(Startup.apiResourceId, credential).Result;
accessToken = result.AccessToken;
}
catch (AdalException ex)
{
}
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = httpClient.GetAsync(Startup.apiResourceId + "/api/ConfApi").Result;
从Webapi端,在Startup.Auth.cs中,我添加了这些代码
public void ConfigureAuth(IAppBuilder app)
{
JwtSecurityTokenHandler.InboundClaimTypeMap.Clear();
app.UseActiveDirectoryFederationServicesBearerAuthentication(
new ActiveDirectoryFederationServicesBearerAuthenticationOptions
{
MetadataEndpoint = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"],
TokenValidationParameters = new TokenValidationParameters() {
SaveSigninToken = true,
ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
}
});
}
但是,在ConfApi控制器中,我找不到任何包含用户详细信息的声明。
如何在Webapi控制器中接收用户详细信息?
感谢您的帮助。
答案 0 :(得分:0)
你真的收到了索赔吗?
您是否在ADFS端配置了Web API的声明规则?
你使用什么名字 - 名字 - 名字,显示名称等?
使用像Fiddler这样的东西来监控流量。在OIDC身份验证之后,您应该会看到访问令牌,ID令牌等。
取得令牌并复制到jwt.io。
这将显示您实际接收的内容。
但是,OWIN类会转换简单的OAuth属性,例如: “争用”到索赔类型URI中,例如http://claims/this-claim所以断点并查看声明集合中的内容以及为每个集合指定的类型。
答案 1 :(得分:0)
对此问题的答案与问题的答案相同:MSIS9649: Received invalid OAuth request. The 'assertion' parameter value is not a valid access token
您必须使用授权代码流(而不是客户端凭据授权流程)来获取服务器应用程序(在这种情况下为Web应用程序)以使用用户的上下文与Web API进行通信。授权代码流将通过JWT令牌中的声明。只需确保通过Web API的RPT索赔发布转换规则中的Web API所需的任何声明。
Vittorio has a nice post on authorization code flow, although it talks about azure.
为了使用授权代码流,您需要通过Startup.ConfigureAuth(IAppBuilder应用程序)中的OpenIdConnectAuthenticationOptions通知处理AuthorizationCodeReceived事件
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions {
...
Notifications = new OpenIdConnectAuthenticationNotifications {
AuthorizationCodeReceived = async code => {
ClientCredential credential = new ClientCredential(Startup.clientId, Startup.appKey);
AuthenticationContext authContext = new AuthenticationContext(Startup.authority, false);
AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
code.Code,
new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)),
credential,
Startup.apiResourceId);
}
}
当您准备好拨打电话时,您将默默获取您的令牌。
var authContext = new AuthenticationContext(Startup.authority, false);
var credential = new ClientCredential(Startup.clientId, Startup.appKey);
var claim = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
var userId = new UserIdentifier(claim, UserIdentifierType.UniqueId);
result = await authContext.AcquireTokenSilentAsync(
Startup.apiResourceId,
credential,
userId);
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer",
result.AccessToken);