正如我在另一个问题中所描述的那样,我构建了一个 Web 服务,该服务将采用用户名/密码并基于这些凭据对 ADFS2 中的用户(移动应用程序)进行身份验证。我的 Web 服务在 ADFS 上配置为 RP。ADFS 颁发 SAML 2.0 令牌。
这是web方法的代码:
public class MobileAuthService : IMobileAuthService
{
private const string adfsBaseAddress = @"https://<my_adfs_hostname>/adfs/services/";
private const string endpointSuffix = @"trust/13/issuedtokenmixedsymmetricbasic256";
public string AuthenticateUser(string username, string password)
{
var binding = new WS2007HttpBinding(SecurityMode.Message);
binding.Security.Message.EstablishSecurityContext = false;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.Security.Mode = SecurityMode.TransportWithMessageCredential;
var trustChannelFactory = new WSTrustChannelFactory(binding, new EndpointAddress(adfsBaseAddress + endpointSuffix))
{
TrustVersion = TrustVersion.WSTrust13
};
trustChannelFactory.Credentials.UserName.UserName = username;
trustChannelFactory.Credentials.UserName.Password = password;
var tokenClient = (WSTrustChannel)trustChannelFactory.CreateChannel();
var rst = new RequestSecurityToken(RequestTypes.Issue, KeyTypes.Symmetric);
var token = tokenClient.Issue(rst);
// do some token-related stuff
return token.Id;
}
}
当我尝试运行它时(来自浏览器的 GET 调用,因为它为此端点配置了 Web http 绑定)我得到以下异常:
System.ServiceModel.Security.MessageSecurityException - "An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail."
内部例外:
System.ServiceModel.FaultException - "An error occurred when verifying security for the message."
我想这与响应签名或证书有关,但我不知道如何克服这个问题,因为我在 WIF 中很新。