【问题标题】:Using WebApi to authenticate with ADFS使用 WebApi 向 ADFS 进行身份验证
【发布时间】:2016-03-21 09:37:07
【问题描述】:

我们正在尝试创建以下场景:

Html 页面向 WebApi 发送 ajax 请求,询问用户是否具有特定的用户角色。 WebApi 从 ADFS 检查用户是否登录(如果没有,WebApi 对用户进行身份验证)。 WebApi 然后从 ADFS 读取用户角色并将 true/false 返回到 html 页面。

我们目前所拥有的:

Ajax 向 WebApi 发送 Get-request。在 WebApi 中,我们有 Authorize-tag,它可以正确地将用户发送到 ADFS-authentication。但是,在身份验证之后,ADFS 将包含 saml 信息的 html 页面返回给客户端而不是 WebApi。所以现在我们创建另一个 ajax 请求(这次是发布),它接收了 html-page 作为数据。 WebApi 然后对其进行解析并根据 SAML 响应中的用户角色返回真/假。

问题:

  1. 当前使用的机制似乎很笨重。这种机制是正确的还是有更好的方法来做到这一点?

  2. 如果我们使用上述方法,是否有可能用户编辑收到的 html 页面并赋予自己他实际上没有的角色?

  3. 即使上述机制是正确的,当 WebApi 重定向到身份验证时,我们仍然会遇到 cors-error。 Cors 在 WebApi 的 Startup.cs 中启用。我们如何摆脱这种情况?

代码:

阿贾克斯:

var uri = 'api/user';
$(document).ready(function () {
        $.ajax({
        url: uri,
        type: "Get",
        success: function (data) {
            $.ajax({
                url: uri,
                type: "POST",
                data: data,
                success: function (value) {
                    if (value == true) {
                            $('#userData').text('You have correct role.');
                    }
                    else {
                            $('#userData').text('You don't have correct role.');
                    }
                },
                error: function (jqXHR, textStatus, err) {
                        window.location.href = "NotLoggedIn.html";
                }
            })
        },
        error: function (jqXHR, textStatus, err) {
                window.location.href = "NotLoggedIn.html";
        }
    });
});

Startup.Auth.cs:

public void ConfigureAuth(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(WsFederationAuthenticationDefaults.AuthenticationType);

    app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
    {
            MetadataAddress = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"],
            Wtrealm = ConfigurationManager.AppSettings["ida:Audience"],

    });
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
            AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType
    });
}

用户控制器:

namespace SSOWebApiSample.Controllers{
    [RoutePrefix("api/user")]
    public class UserController : ApiController
    {
            [HttpGet]
            [Route("")]
            [Authorize]
            public IHttpActionResult GetUser()
            {
                    return Ok();
            }

            [HttpPost]
            [Route("")]
            public async Task<IHttpActionResult> PostUser()
            {
                    bool isAdditionalInfoAllowedUser = false;
                    string result = await Request.Content.ReadAsStringAsync();
                    //Parse result here
                    return Ok(isAdditionalInfoAllowedUser);
            }
    }
}

【问题讨论】:

    标签: c# asp.net ajax asp.net-web-api adfs


    【解决方案1】:

    AdalJS 会很好地清理它。请参考以下内容:

    1. To Do application adapted from Azure AD to ADFS
    2. Azure AD sample with CORS
    3. Enabling Cross-Origin Requests in ASP.NET Web API 2

    为了通过 ADFS 身份验证成功调用 CORS Web API,我发现在我的 Angular 应用程序配置中调用 adalAuthenticationService.init() 时需要设置实例、租户、clientId 和 endpoints 成员。有关端点示例,请参见示例 2,但将 GUID 替换为 URL。

    【讨论】:

      猜你喜欢
      • 2013-08-31
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多