在WIF中处理并发身份提供者声明

时间:2014-12-26 03:57:06

标签: asp.net authentication azure wif claims

我的ASP.NET MVC应用程序正在使用WIF 3.5和Azure ACS与Google,Facebook等进行联合身份验证。在注册期间,用户通过IdP进行身份验证后,我们会将自定义用户数据存储在我们的数据库中。 / p>

目前,它允许一个用户帐户与一个身份提供商关联,但我要求为一个帐户添加对多个身份提供商的支持(.ie用户可以将Google,Facebook等添加到他的帐户中他可以使用这些IdP中的任何一个登录我们的网站)。要向其帐户添加其他IdP,用户需要先登录主IdP。序列是这样的:

  • 用户进入登录页面,选择一个IdP(例如Google),登录Google,Azure ACS在身份验证后重定向回我们的网站
  • 在个人资料页面中,用户选择添加其他IDP,他被重定向到页面以选择IdP
  • 他点击另一个IdP(例如Facebook),登录Facebook,Azure ACS重定向回我们的网站(我可以看到来自Facebook的所有正确声明,Azure ACS传回我们的网站)
  • 问题是我不确定如何提取这些Facebook声明。如果我使用

    HttpContext.Current.User.Identity as Microsoft.IdentityModel.Claims.IClaimsIdentity

它返回Google(用户登录时的当前IdP)声明

1 个答案:

答案 0 :(得分:1)

最后,我需要阅读回复并手动将其解析为声明,这里我是如何做的,仅供参考:

//Response from Azure ACS is stored in wresult parameter
string samlResponse = Request.Params["wresult"];

var xmlTextReader = new XmlTextReader(new StringReader(samlResponse));

//Only interested in Assertion section of the response
xmlTextReader.ReadToFollowing("Assertion", "urn:oasis:names:tc:SAML:2.0:assertion");

SecurityTokenHandlerCollection handlers = FederatedAuthentication.ServiceConfiguration.SecurityTokenHandlers;

// read the token
var securityToken = handlers.ReadToken(xmlTextReader);

// validate the token and create the identity collection
var claimsIdentityCollection = handlers.ValidateToken(securityToken);

// create ClaimsPrincipal
var claimsPrincipal = new ClaimsPrincipal(claimsIdentityCollection); 

虽然它有效,但我不确定这是否是最好的方法,所以我仍然会为那些有更好答案的人留下问题