Dam*_*ver 8 c# adfs2.0 .net-4.5 visual-studio-2012
我正在尝试构建一个使用ADFS和声明的系统.目前,这只是一个"玩具"实施.
我已经构建了一个非常简单的MVC Web应用程序,使用Visual Studio中的"Identity and Access ..."向导进行设置,以便与ADFS 2.0服务器通信,并将其部署到IIS服务器.一切正常,我可以检查并列出收到的索赔.
下一步是构建基于Web API的REST服务(表示MVC应用程序将依赖的后端服务),因此我希望将凭据传递给该后端服务器,以便它可以进行适当的授权决定.
所以第一步是让我创建委托令牌(然后,我希望能够根据HttpClient
类来计算如何处理它以进行其余的调用).我有这个:
//We need to take the bootstrap token and create an appropriate ActAs token
var rst = new RequestSecurityToken
{
AppliesTo = new EndpointReference("https://other-iis.example.com/Rest"),
RequestType = RequestTypes.Issue,
KeyType = KeyTypes.Symmetric,
ActAs = new SecurityTokenElement(((BootstrapContext)((ClaimsIdentity)User.Identity).BootstrapContext).SecurityToken)
};
var sts = new SecurityTokenService(); //This line isn't valid
var resp = sts.Issue(System.Threading.Thread.CurrentPrincipal as ClaimsPrincipal, rst);
Run Code Online (Sandbox Code Playgroud)
但是,问题在于它SecurityTokenService
是抽象的.我找不到从这个类派生的任何类型的任何System.IdentityModel
也不System.IdentityModel.Services
及上述不包括的ADFS服务器,我显然需要在某些时候提供任何参考.
当然,我也可能完全走错了路线,或者只是遇到了一个小绊脚石而且看不到远处有一个更大的一个,所以任何有关这方面的建议也会受到赞赏.
我已经看过,例如,Identity Delegation Scenario,但是CreateChannelActingAs
当我正在与休息服务交谈时,我认为它不会起作用(或者它会吗?),而且似乎也没有适用于.NET 4.5.
我正在从 ADFS 2.0 请求令牌进行缓存并查看 DisplayToken。也许这可以帮助您入门。
这是我能想到的:
public SecurityToken GetToken(out RequestSecurityTokenResponse rstr)
{
Console.WriteLine("Connecting to STS...");
WSTrustChannelFactory factory = null;
try
{
if (_useCredentials)
{
// use a UserName Trust Binding for username authentication
factory =
new WSTrustChannelFactory(
new UserNameWSTrustBinding(SecurityMode.TransportWithMessageCredential),
"https://<adfs>/adfs/services/trust/13/UsernameMixed");
factory.TrustVersion = TrustVersion.WSTrust13;
// Username and Password here...
factory.Credentials.UserName.UserName = "username";
factory.Credentials.UserName.Password = "password";
}
else
{
// Windows authentication over transport security
factory = new WSTrustChannelFactory(
new WindowsWSTrustBinding(SecurityMode.Transport),
"https://<adfs>/adfs/services/trust/13/windowstransport") { TrustVersion = TrustVersion.WSTrust13 };
}
var rst = new RequestSecurityToken
{
RequestType = RequestTypes.Issue,
AppliesTo = SvcEndpoint,
KeyType = KeyTypes.Symmetric,
RequestDisplayToken = true
};
Console.WriteLine("Creating channel for STS...");
IWSTrustChannelContract channel = factory.CreateChannel();
Console.WriteLine("Requesting token from " + StsEndpoint.Uri);
SecurityToken token = channel.Issue(rst, out rstr);
Console.WriteLine("Received token from " + StsEndpoint.Uri);
return token;
}
finally
{
if (factory != null)
{
try
{
factory.Close();
}
catch (CommunicationObjectFaultedException)
{
factory.Abort();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想使用 ADFS 2.0,您可能必须激活 UsernameMixed Endpoint,并且之后不要忘记重新启动服务!