在默認配置(客戶端& STS)中,您獲得的令牌將被加密(除了被簽名以外)。如果你擁有整個事物(客戶端和服務),那麼你可以調整一些旋鈕,使得令牌可以從客戶端「讀取」(因此,不加密)。
這裏有一些代碼會給你一個來自ADFS的未加密的SAML令牌(關鍵是要求一個「承載」令牌並且配置沒有加密證書的ADFS依賴方)。
private static SecurityToken GetSamlToken(string realm, string stsEndpoint, ClientCredentials clientCredentials)
{
using (var factory = new WSTrustChannelFactory(
new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential),
new EndpointAddress(new Uri(stsEndpoint))))
{
factory.Credentials.UserName.UserName = clientCredentials.UserName.UserName;
factory.Credentials.UserName.Password = clientCredentials.UserName.Password;
factory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
factory.TrustVersion = TrustVersion.WSTrust13;
WSTrustChannel channel = null;
try
{
var rst = new RequestSecurityToken
{
RequestType = WSTrust13Constants.RequestTypes.Issue,
AppliesTo = new EndpointAddress(realm),
KeyType = KeyTypes.Bearer,
};
channel = (WSTrustChannel)factory.CreateChannel();
return channel.Issue(rst);
}
finally
{
if (channel != null)
{
channel.Abort();
}
factory.Abort();
}
}
一旦你有,你可以使用LINQ到XML或WIF得到ClaimsIdentity出SecurityToken的令牌。確保您在客戶端和STS以及服務之間使用SSL。
您擁有的第二個選擇是依靠服務返回索賠列表。這是一個更多的請求,但您將在用戶登錄的同時執行此操作,然後緩存這些聲明,直到令牌過期。
public IEnumerable<Claim> GetUserClaims() {
// get Thread.CurrentPricinpal IClaimsIdentity and grab the claims
}
非常感謝您的詳細代碼。我知道有很多部分我都不太瞭解。但是你的代碼對於指導我非常有幫助! :) – Gope 2012-07-16 13:54:48
這正是我正在尋找的 - 從桌面應用程序獲取授予用戶名/密碼的令牌。但是,當我運行這個時,我一直從服務器獲得「(405)方法不允許」。任何指針? – atlantis 2013-04-05 07:54:50