我正在嘗試創建一個應用程序,要求通過活動目錄進行用戶身份驗證才能返回令牌,但我不確定如何正確使用它。使用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工具