2015-04-07 159 views

回答

1

这取决于您是使用WIF还是使用.NET 4.5 System.IdentityModel

使用WIF:

string endpointUri = string.Format("https://{0}/adfs/services/trust/13/usernamemixed", _serverName); 

var factory = new Microsoft.IdentityModel.Protocols.WSTrust.WSTrustChannelFactory(
       new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential), 
       new EndpointAddress(endpointUri)); 

factory.TrustVersion = TrustVersion.WSTrust13; 
if (factory.Credentials != null) 
{ 
    factory.Credentials.UserName.UserName = "UserName"; 
    factory.Credentials.UserName.Password = "password"; 
} 

var rst = new RequestSecurityToken 
{ 
    RequestType = WSTrust13Constants.RequestTypes.Issue, 
    AppliesTo = new EndpointAddress(_relyingPartyUri), 
    KeyType = WSTrust13Constants.KeyTypes.Bearer, 
}; 

var channel = factory.CreateChannel(); 
SecurityToken token = channel.Issue(rst); 
return token; 
1

使用.NET 4.5 System.IdentityModel,你需要定义自己UserNameWSTrustBinding:

public class UserNameWSTrustBinding : WS2007HttpBinding 
{ 
    public UserNameWSTrustBinding() 
    { 
     Security.Mode = SecurityMode.TransportWithMessageCredential; 
     Security.Message.EstablishSecurityContext = false; 
     Security.Message.ClientCredentialType = MessageCredentialType.UserName; 
    } 
} 

string endpointUri = string.Format("https://{0}/adfs/services/trust/13/usernamemixed", _serverName); 

var factory = new WSTrustChannelFactory(new UserNameWSTrustBinding(), endpointUri) 
    { 
     TrustVersion = TrustVersion.WSTrust13 
    }; 

factory.Credentials.UserName.UserName = "UserName"; 
factory.Credentials.UserName.Password = "password"; 

var rst = new RequestSecurityToken 
{ 
    RequestType = RequestTypes.Issue, 
    AppliesTo = new EndpointReference(_relyingPartyUri), 
    KeyType = KeyTypes.Symmetric 
}; 

var channel = factory.CreateChannel(); 

return channel.Issue(rst); 
1

这取决于你正在使用的应用程序类型。 使用WIF对ADFS进行身份验证有两种形式: - 使用Asp.net Web表单或MVC进行被动身份验证。你可以参考这篇文章:Claims Aware MVC4 App using WIF Identity and Access tool in .Net 4.5

这也取决于您使用的是.NET框架,您将需要下载下列之一: - WIF运行和WIF SDK用于.NET 4.0 - 身份和访问.NET 4.5工具

相关问题