0

我構建了以asp.net核心web api作爲後端的xamarin移動應用程序,並且我想使用oauth2.0授權碼流保護它。Azure AD B2C授權碼流程

使用該文檔 - https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-reference-oauth-code#1-get-an-authorization-code

我試圖獲取授權碼。我發送請求到https://login.microsoftonline.com/ {tenant_domain_name} /oauth2/v2.0/authorize?端點與查詢參數:

client_id - id of added application to azure ad b2c 
response_type=code 
redirect_uri=redirect url for native application from added application to azure ad b2c 
response_mode=query 
scope=client id value and "offline_access" separated by whitespace state=test_0001 
p=B2C_1_CommonLogin - sign in policy for local accounts 

從響應我得到html頁面登錄爲字符串。我解析登錄頁面字符串以獲取ctx,flowToken和canary標記以發送請求到https://login.microsoftonline.com/ {tenant_domain_name} /登錄端點以獲取授權代碼。 進行POST請求,我使用以下格式:

string postData = string.Format([email protected]"login={email}&passwd={password}&ctx={ctx}&flowToken={flowToken}&canary={canary}&dssoToken="); 
byte[] data = Encoding.UTF8.GetBytes(postData); 

和寫入數據陣列來請求流。

然後我在請求對象上調用GetResponse,並獲取「不良請求」作爲響應而沒有詳細信息。 同樣在HttpWebReponse對象中,我看到指向的ResponseUrl: https://login.microsoftonline.com/te/ {tenant_domain_name}/oauth2/authresp? 並在查詢中包含id_token,state和session_state參數。

你能建議什麼嗎?

回答

0

您是否使用代碼來模擬登錄進度?輸入用戶名/密碼以將其發佈到Azure登錄端點後,Azure AD應該給出302響應,這將在您傳遞請求時重定向URL。

而且您可以在位置頭部找到代碼,然後您可以使用代碼交換令牌作爲提及的文檔。這裏是一個成功的迴應供你參考: enter image description here

爲了縮小這個問題,我建議你嘗試在使用提琴手的大衣和帽子的請求。

+0

是的,我試圖模擬它,但它並不像我想的那麼容易。我可以在瀏覽器運行登錄策略中重現相同的錯誤(錯誤的請求)。因爲登錄後我的登錄策略是針對本地應用的,所以邊緣嘗試打開某個應用,如果在此階段刷新瀏覽器頁面並在更多時間登錄,則會收到錯誤的請求。我想這是對某事的保護。我的代碼請求被相同的保護阻止。 – Crossman