【问题标题】:How do you extract and validate a SAML token in the Authorization header using ASP.NET Web API?如何使用 ASP.NET Web API 提取和验证 Authorization 标头中的 SAML 令牌?
【发布时间】:2012-03-19 14:10:07
【问题描述】:

我有一个 Silverlight 应用程序,它使用 Identity Training Kit 中的 WSTrust 实现从 STS 请求 SAML 令牌。

private void Application_Startup(object sender, StartupEventArgs e)
{
    WSTrustClient wsTrustClient = new WSTrustClient(new WSTrustBindingUsernameMixed(), new EndpointAddress("https://localhost/SecurityTokenService/Service.svc/IWSTrust13"), new UsernameCredentials("user", "password"));
    wsTrustClient.IssueCompleted += new EventHandler<IssueCompletedEventArgs>(wsTrustClient_IssueCompleted);

    RequestSecurityToken rst = new RequestSecurityToken()
    {
        AppliesTo = new EndpointAddress("https://localhost/SilverlightApplication.Web")
    };

    wsTrustClient.IssueAsync(rst); 
}

private void wsTrustClient_IssueCompleted(object sender, IssueCompletedEventArgs e)
{
    this.Resources.Add("SamlToken", e.Result);
    this.RootVisual = new MainPage();
}

然后,Silverlight 应用程序将 SAML 令牌放入对我的 ASP.NET Web API 服务的请求的授权标头中;再次使用相同的 WSTrust 实现。

string uri ="https://localhost/WebApi/api/resource";
WebRequest request = WebRequest.Create(uri);
request.Method = "GET";

RequestSecurityTokenResponse rstr = (RequestSecurityTokenResponse)Application.Current.Resources["SamlToken"];
request.Headers[HttpRequestHeader.Authorization] = "SAML " + rstr.RequestedSecurityToken.RawToken;

request.BeginGetResponse((result) =>
{
    using (WebResponse response = request.EndGetResponse(result))
    {
        // Process Response
    }
}, null);

首先,我这样做是否正确?其次,如果是这样,我如何让我的 ASP.NET Web API 服务检测 SAML 令牌并将其转换为 IClaimsPrincipal 服务器端?

【问题讨论】:

    标签: c# rest asp.net-web-api wif saml


    【解决方案1】:

    不是 Silverlight 专家,但这里有一系列关于 WebAPI 服务和 WIF 的文章(和一些自定义类) - ASP.NET WebAPI Security 1: Introducing Thinktecture.IdentityModel.Http

    这可能有帮助吗?

    【讨论】:

    • 谢谢。我在 Dominick Baier 的博客上花了很多时间,他广泛使用了他自己的 Thinktecture 框架,但我自己永远无法找到如何去做。至少现在我可以检查他的源代码,看看他做了什么。
    猜你喜欢
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    相关资源
    最近更新 更多