我正在尝试创建一个应用程序,要求通过活动目录进行用户身份验证才能返回令牌,但我不确定如何正确使用它。使用Active Directory在C#中进行身份验证
我一直在寻找Authenticate user by ADFS (Active Directory Federation Service),但我不知道如何创建一个请求安全令牌或如何正确使用它。
有没有可用的工作示例?任何帮助表示赞赏。
我正在尝试创建一个应用程序,要求通过活动目录进行用户身份验证才能返回令牌,但我不确定如何正确使用它。使用Active Directory在C#中进行身份验证
我一直在寻找Authenticate user by ADFS (Active Directory Federation Service),但我不知道如何创建一个请求安全令牌或如何正确使用它。
有没有可用的工作示例?任何帮助表示赞赏。
这取决于您是使用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;
使用.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);
这取决于你正在使用的应用程序类型。 使用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工具