2012-10-24 15 views
1

目前,我正在使用OpenID(通過C#和DotNetOpenAuth)在應用程序A('其應用程序')和應用程序B('我的應用程序')之間創建單一登錄功能。在DotNetOpenAuth中強制使用不同的屬性交換名稱空間

我希望利用屬性交換來獲取其應用程序提供的數據。他們的API文檔AX請求應該是什麼樣子:

openid.ns.ax    = http://openid.net/srv/ax/1.0 
openid.ax.type.studentids = http://theirapp.com/path/student-ids 

注意.ax命名空間。 That seems in line with OpenID's AX specs.

這是我如何使用DotNetOpenAuth申請和獲得的屬性,as suggested by SO和一個極大其它來源:

FetchRequest ax = new FetchRequest(); 
ax.Attributes.AddRequired("http://theirapp.com/path/student-ids"); 
request.AddExtension(ax); 

string sIDs = String.Empty; 
if (fetch != null) 
    sIDs = fetch.GetAttributeValue("http://theirapp.com/path/student-ids"); 

我困惑時的反應回來完全不理AX要求。觀察查詢字符串參數我們送他們之後,我想它實際上不是他們的錯:

openid.ns:http://specs.openid.net/auth/2.0 
openid.ns.alias3:http://openid.net/srv/ax/1.0 
openid.alias3.required:alias1 
openid.alias3.mode:fetch_request 
openid.alias3.type.alias1:http://theirapp.com/path/student-ids 
openid.alias3.count.alias1:1 

什麼是地獄,DotNetOpenAuth? 'alias3'從哪裏來?這應該是'斧頭'。我無法判斷我正在使用的應用程序是否過分抨擊AX名稱空間,或者DotNetOpenAuth沒有注意強制OpenID協議。

所以,畢竟這個瘤子,我的問題:

  1. 我需要我的AX請求在命名空間openid.ax,不openid.alias3。我如何強制DotNetOpenAuth來做到這一點?
  2. 該API要求所請求的屬性被命名爲 - 在這種情況下,'student-ids'。上面,他們得到了默認名稱,如'alias1'。如何強制DotNetOpenAuth使用自定義名稱標記屬性?
  3. 誰在這裏:需要openid.ax或DotNetOpenAuth不關心的API?

回答

1

感謝spamguy。我想出了一個辦法解決這個問題,並將其發佈給其他人,可能會遇到與PowerSchool的OpenID提供者的侷限性相同的問題。

您需要使用DotNetOpenAuth正常構建請求,然後從請求中提取重定向響應。一旦你有了這個,你可以拖動Location頭並根據需要覆蓋命名空間值。

本示例適用於MVC應用程序。如果您需要在WebForms應用程序中執行此操作,只需從方法中取出邏輯並將return redirectResponse.AsActionResult();替換爲redirectResponse.Send()即可。您將需要在通常的try ... catch ... for ThreadAbortExceptions中包裝它。使用

命名空間:

using System; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Mvc; 
using System.Web.Security; 
using DotNetOpenAuth.Messaging; 
using DotNetOpenAuth.OpenId; 
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange; 
using DotNetOpenAuth.OpenId.RelyingParty; 

黑客:

const string NS_DCID = "http://powerschool.com/entity/id"; 
const string NS_USERTYPE = "http://powerschool.com/entity/type"; 

static readonly OpenIdRelyingParty openid = new OpenIdRelyingParty(); 

private ActionResult SubmitOpenIdRequest(string openid_identifier) 
{ 
    var request = openid.CreateRequest(openid_identifier); 

    var ax = new FetchRequest(); 

    // any modification to the following attributes will require a corresponding change in the string replacement hack 
    ax.Attributes.AddRequired(NS_DCID); 
    ax.Attributes.AddRequired(NS_USERTYPE); 

    request.AddExtension(ax); 

    var redirectResponse = request.RedirectingResponse; 

    // PowerSchool is violating the attribute exchange specification, requiring specific namespaces and elements to function 
    // Need to overwrite the values generated by OpenID to patch PowerSchool's incorrect provider implementation 
    redirectResponse.Headers["Location"] = redirectResponse.Headers["Location"] 
     .Replace("openid.ns.alias3", "openid.ns.ax") 
     .Replace("openid.alias3.required=dcid%2Calias2", "openid.ax.required=dcid%2Cusertype") 
     .Replace("openid.alias3.mode", "openid.ax.mode") 
     .Replace("openid.alias3.type.alias1", "openid.ax.type.dcid") 
     .Replace("openid.alias3.count.alias1", "openid.ax.count.dcid") 
     .Replace("openid.alias3.type.alias2", "openid.ax.type.usertype") 
     .Replace("openid.alias3.count.alias2", "openid.ax.count.usertype"); 

    return redirectResponse.AsActionResult(); 
} 

被警告:這是一個脆弱的黑客。 DNOA中的內部更改或對FetchRequest的更改可能會破壞替換邏輯。

2

問題1:

我需要我的AX請求在命名空間openid.ax,不openid.alias3。我如何強制DotNetOpenAuth來做到這一點?

DotNetOpenAuth不允許您強制使用特定擴展名的別名。要求特定的一個與OpenID 2.0規範相反。即使是AX extension itself states this in section 1.1

openid.ns.<extension_alias>=http://openid.net/srv/ax/1.0

實際擴展命名空間別名應該基於每個消息的基礎的一方構成的消息,以這樣的方式來確定以避免多個擴展之間的衝突。就本文而言,屬性交換服務的擴展名稱空間別名將爲「ax」。

問題2:

的API需要請求屬性被命名只是如此 - 在這種情況下, '學生的IDS'。上面,他們得到了默認名稱,如'alias1'。如何強制DotNetOpenAuth使用自定義名稱標記屬性?

就像你的第一個問題,DotNetOpenAuth不提供的方式爲您強制各個屬性的別名,因爲AX擴展不允許這些類型的需求。該屬性的類型在URI中給出。該屬性的別名可以由依賴方定義,並且OpenID提供者應該查看屬性的類型URI,並接受來自依賴方的任何別名。

問題3:

誰是這裏的權利:對於需要openid.ax或DotNetOpenAuth不關心的API?

DotNetOpenAuth是正確的。如果你正在查看的這個student-id交換文檔的別名區別與什麼DotNetOpenAuth是你所看到的AX屬性被忽略的問題的根源,那麼錯誤在於一個糟糕的OpenID提供者正在服務器上運行。 OpenID提供商的糟糕實現非常可怕 - 不僅僅是因爲糟糕的互操作性,而是因爲他們認爲可能存在許多可能的安全問題,並且在正確實施規範時缺乏謹慎。

我建議您嘗試使用Fiddler或其他HTTP嗅探器攔截出站請求,修復所有別名以匹配文檔建議的應該是什麼,並查看它是否修復了問題。如果是這樣,OpenID提供商應該是固定的,你可以聯繫他們,要求他們升級到他們正在使用的任何庫的更新版本(希望能獨自解決它)。如果「修復」別名而不是解決了問題,那麼您可以繼續調查互操作失敗的其他可能原因。

+1

後續:使用Fiddler來嘗試ax命名空間是一個好主意。它最終證明提供者期望a)AX中的請求和b)明確命名的屬性。他們生氣地說,一個解決方案即將到來......最終。在那之前我不確定我會做什麼...... – spamguy

+0

+1 Thanks Andrew。如果沒有你的答案,找到解決方法本來是一條漫長的道路。 – Brandon

+0

確實。如果可以的話,我會接受布蘭登和你的答案。 – spamguy

相關問題