3

我有一个 MVC 应用程序,它登录到 wsFederation STS(到目前为止我使用的是 Identity Server v2)。在这种情况下一切正常。我为此使用了 OWIN 身份验证中间件。

 app.UseCookieAuthentication(new CookieAuthenticationOptions
 {
      AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType,
      CookieName = ".AspNet.Federation"
 });

 //Trusted Issuer
 parameters.ValidIssuer = "http://identityserver.v2.thinktecture.com/trust/localSTS";

 var options = new WsFederationAuthenticationOptions
 {
      MetadataAddress = "https://IdSrv/FederationMetadata/2007-06/FederationMetadata.xml",
      Wtrealm = "urn:relyingparty2",
      IssuerAddress = "https://IdSrv/issue/wsfed",
      TokenValidationParameters = someparameters,
 };

 app.UseWsFederationAuthentication(options);

现在,我有另一个应用程序,它是 Web API 应用程序,托管在另一个域上。

我希望在 MVC 应用程序上经过身份验证的用户能够从该应用程序向 Web api 进行 Ajax 调用。MVC 应用程序如何将用户传递给 web api?

我尝试在 ajax 调用上设置withCredentials选项,以将身份验证 cookie 传递给 webapi 服务。

var request = {
            url: 'https://localhost:44305/api/test/GetStrings',
            xhrFields: {
                withCredentials: true
            },
            async: true,
            type: 'GET',
            crossDomain: true,
            success:
            function () {
                alert('ok');
            },
            error: function () {
                alert('ko');
            }
        };

        $.ajax(request);

这是 web api 控制器,配置为启用 CORS(工作正常)

[EnableCors(origins: "https://localhost,http://localhost", headers: "*", methods: "*", SupportsCredentials = true)]
public class TestController : ApiController
{
    [HttpGet]
    [Authorize]
    public IEnumerable<string> GetStrings()
    {
        return new string[] { "value1", "value2" };
    }
}

但是当涉及到 webapi 配置时,我不知道该怎么做。使用 Web 应用程序不起作用的相同配置,因为请求被重定向到 STS 以进行令牌发布。

有什么想法吗 ?这是使用正确的模式吗?

我也在网上看到,有些人会把从 STS 收到的 SAML 令牌作为不记名令牌传递到 ajax 调用的授权标头中,但通常说不推荐。

4

0 回答 0