使用WebApi通过ADFS进行身份验证

时间:2016-03-21 09:37:08

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

我们正在尝试创建以下方案:

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

到目前为止我们有什么:

Ajax将Get-request发送给WebApi。在WebApi中,我们有Authorize-tag,它正确地将用户发送到ADFS身份验证。但是,在身份验证之后,ADFS会将包含saml信息的html-page返回给客户端而不是WebApi。所以现在我们创建另一个ajax请求(此次发布),它已经收到html-page作为数据。 WebApi然后解析它并根据SAML响应中的用户角色返回true / false。

问题:

  1. 目前使用的机制似乎很笨重。这种机制是否正确或有更好的方法吗?

  2. 如果我们使用上述方法,是否有可能用户编辑收到的html-page并赋予自己不具备的角色?

  3. 即使上述机制正确,当WebApi重定向到身份验证时,我们仍然会出现cors-error。 Cors在WebApi的Startup.cs中启用。我们如何摆脱这个?
  4. 代码:

    的Ajax:

    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
        });
    }
    

    UserController中:

    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);
                }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

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
  4. 要使用ADFS身份验证成功进行CORS Web API调用,我发现在Angular应用程序中调用adalAuthenticationService.init()时需要设置实例,租户,clientId和端点成员配置。有关端点示例,请参阅示例二,但将GUID替换为URL。

相关问题