2013-02-25 134 views
0

如何更改來自具有ClaimsAuthenticationManager的acs的原始標記值.I要將角色添加到token.i設法將該角色添加到claimsidentity中,但它未反映在原始標記中。使用聲明的自定義授權授權管理器

string rawToken = string.Empty; 

ClaimsIdentity identity = HttpContext.User.Identity as ClaimsIdentity; 

if (null != identity) 
{ 
    SimpleWebToken token = identity.BootstrapToken as SimpleWebToken; 

    if (null != token) 
    { 
     rawToken = token.RawToken; 
    } 
    } 

作用體現在身份,但它不是在引導令牌獲取添加。

回答

0

自舉令牌用於構造ClaimIdentity。在ClaimsAuthenticationManager中,您已經擁有從ACS令牌創建的incomingPrincipal。另外,令牌內部的權利要求由ACS簽署,理論上不能在令牌消費者沒有驗證問題的情況下進行更改。

你試圖實施什麼場景?重用WCF服務或類似的令牌?

UPDATE 下面是一個示例代碼,用於從ACS(帶有SAML2標記)更改標記。 注意:具有不同數據的SAML斷言具有不同的ID是非常重要的。如果實現一個方案,加載一個斷言「模板」並填充某些數據位,則必須更改Id,否則在調試器中將看到修改後的斷言值,但WriteToken方法將從存儲在令牌中的源字節中寫入原始未修改的令牌。

X509Certificate2 singingCertificate = new X509Certificate2(certificateFile, certificatePassword); 
Saml2SecurityTokenHandler handler = CreateTokenHandler(); 
Saml2SecurityToken baseToken = GetTemplateToken(); 
Saml2Assertion assertion = templateToken.Assertion; 

//modify template token - change date, add claims etc 
assertion.Id = new Saml2Id(); 
//order is important, because in sampleToken this property is already setup and NotBefore date can not be NotOnOrAfter 
assertion.Conditions.NotOnOrAfter = DateTime.MaxValue; 
assertion.Conditions.NotBefore = DateTime.UtcNow; 

//prepare to resign token assertions 
X509AsymmetricSecurityKey signingKey = new X509AsymmetricSecurityKey(singingCertificate); 
X509RawDataKeyIdentifierClause x509clause = new X509RawDataKeyIdentifierClause(singingCertificate); 
SecurityKeyIdentifier keyIdentifier = new SecurityKeyIdentifier(new SecurityKeyIdentifierClause[] { x509clause }); 
assertion.SigningCredentials = new SigningCredentials 
    (
    signingKey, 
    assertion.SigningCredentials.SignatureAlgorithm, 
    assertion.SigningCredentials.DigestAlgorithm, 
    keyIdentifier 
    ); 
//create and sign modified token 
Saml2SecurityToken token = new Saml2SecurityToken(assertion, new ReadOnlyCollection<SecurityKey>(new List<SecurityKey>() { signingKey }), templateToken.IssuerToken); 
+0

http://code.msdn.microsoft.com/windowsazure/ASPNET-Security-SWT-With-a0183e7a/view/SourceCode#content – tony 2013-02-26 05:42:43

+0

http://code.msdn.microsoft.com/windowsazure/ ASPNET-Security-SWT-With-a0183e7a/view/SourceCode#content(這是我正在嘗試實現的).i在與msdn link.now中使用相同的方法做了驗證部分,現在我試圖授權用戶在休息場景在服務器端。爲此,我使用claimauthenticationmanager來修改權利要求。我能夠將角色添加到聲明值,但它不反映到來自acs的令牌。只是我設法在claimsidentity中添加角色。 – tony 2013-02-26 05:48:24

+0

如果您確實想要更改令牌,則必須在更改後使用ACS證書的私鑰退出令牌。或者你可以在服務端禁用令牌驗證,但這將意味着令牌只是作爲標準容器來使用。 – 2013-02-27 05:39:11