[英]Unable to obtain username from WebAPI in ADFS4-OAuth2 OpenID
我正在嘗試通過OAuth使用ADFS身份驗證在我的webapp和webapi之間進行通信。 我正在使用ADFS4,並已使用服務器應用程序和Webapi相應地配置了應用程序組。 我正在嘗試接收用戶詳細信息,尤其是來自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控制器中接收用戶詳細信息?
謝謝你的幫助。
您實際上是在收到索賠嗎?
您是否在ADFS端為Web API配置了聲明規則?
您用於名稱的什么-名稱,顯示名稱等?
使用Fiddler之類的工具監視流量。 在OIDC身份驗證之后,您應該看到訪問令牌,ID令牌等。
獲取令牌並將其復制到jwt.io。
這將顯示您實際收到的內容。
但是,OWIN類將簡單的OAuth屬性(例如“ aud”)轉換為聲明類型URI(例如http:// claims / this-claim),以便斷點並查看聲明集合中的內容以及已為每個聲明類型分配的類型。
答案與對問題的答案相同: MSIS9649:收到無效的OAuth請求。 “ assertion”參數值不是有效的訪問令牌
您必須使用授權代碼流(而不是客戶端憑據授予流)來使服務器應用程序(在這種情況下為Web應用程序)與用戶的上下文與Web API進行通信。 授權代碼流將通過JWT令牌傳遞聲明。 只要確保您通過了Web API的RPT索賠發布轉換規則中對Web API所需的所有索賠即可。
Vittorio關於授權代碼流有一個不錯的帖子,盡管它談到了蔚藍。
為了使用授權代碼流,您需要通過Startup.ConfigureAuth(IAppBuilder app)的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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.