validationSWT令牌REST WCF服务

我目前正在开发一个WPF客户端,它从Windows Azure AppFabric ACS获取SWT令牌。 有了这个令牌,我想使用RESTful WCF服务。 我使用本教程获取SWT令牌,它完美无缺。 在这个MSDN教程的帮助下,我创建了RESTful WCF服务。

问题是令牌可能具有错误的格式,因为令牌validation器无法validation它(令牌validation器的IsHMACValid方法中的错误,swtWithSignatur.Length == 1)。

与服务器联系的令牌示例:

{"appliesTo":"http://localhost:7100/Service/Default.aspx","context":null,"created":1326996221,"expires":1326999821,"securityToken":"<?xml version="1.0" encoding="utf-16"?><wsse:BinarySecurityToken wsu:Id="uuid:74ba5667-04ea-4074-9544-aaafb570c648" ValueType="http://schemas.xmlsoap.org/ws/2009/11/swt-token-profile-1.0" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">aHR</wsse:BinarySecurityToken>","tokenType":"http://schemas.xmlsoap.org/ws/2009/11/swt-token-profile-1.0"}

在Windows Azure管理门户中,我选择SWT作为我的依赖方应用程序的令牌格式。 根据第一个教程,SWT令牌的格式看起来不错,但令牌validation器不会接受它。

PS:如果有人正在尝试第二个教程(如何:使用ACS部署到Windows Azure的REST WCF服务进行身份validation):我认为步骤3中的第11点存在错误,您必须修改web.config文件( system/webService部分不存在)。 配置应如下所示:

         

我发送到服务器的令牌格式错误。 上面的标记是json格式,包含一个’securityToken’,它是xml编码的。 使用HttpUtility.UrlDecodeXMLReader ,可以检索base64字符串。 上述标记的base64字符串是:

aHR

我解码了这个字符串并得到了我的ACS令牌。 此ACS令牌现在有效,可以使用我的RESTful WCF服务。

服务器端的代码没有改变。 这就是我在客户端获得的:

 // parse the token from the json string, var token = JsonNotifyRequestSecurityTokenResponse.FromJson(txtReceivedToken.Text); // get the security token and decode it string xmlString = HttpUtility.UrlDecode(token.SecurityTokenString); // get the base64 string an string string64 = ""; using (XmlReader xmlReader = XmlReader.Create(new StringReader(xmlString))) { while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Text) { // find the first text element, which should be the base64 string string64 = xmlReader.Value; break; } } } // decode it string acsToken = base64Decode(string64); // set the header string headerValue = string.Format("WRAP access_token=\"{0}\"", acsToken); client.Headers.Add("Authorization", headerValue); Stream stream = client.OpenRead(@"http://127.0.0.1:81/Service1.svc/users"); StreamReader reader = new StreamReader(stream); String response = reader.ReadToEnd(); 

base64Decode方法我从http://www.vbforums.com/showthread.php?t=287324偷走了。 我从http://www.leastprivilege.com/获得的JsonNotifyRequestSecurityTokenResponse.FromJson部分,但我认为它可以使用任何可用的JSON解析器进行解析。

我不知道它是否是最佳解决方案,但它对我有用。