簡體   English   中英

使用WebApi通過ADFS進行身份驗證

[英]Using WebApi to authenticate with ADFS

我們正在嘗試創建以下場景:

HTML頁面向WebApi發送ajax請求,詢問用戶是否具有某些用戶角色。 WebApi從ADFS檢查用戶是否已登錄(如果沒有,則WebApi對用戶進行身份驗證)。 然后,WebApi從ADFS讀取用戶角色並將true / false返回到html頁面。

到目前為止,我們擁有:

Ajax將Get-request發送到WebApi。 在WebApi中,我們具有Authorize標簽,該標簽可以正確地將用戶發送到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中啟用。 我們怎么擺脫這個?

代碼:

阿賈克斯:

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

AdalJS會很好地清理它。 請參考以下內容:

  1. 待從Azure AD到ADFS的應用程序
  2. 帶有CORS的Azure AD示例
  3. 在ASP.NET Web API 2中啟用跨源請求

為了使用ADFS身份驗證成功進行CORS Web API調用,我發現在Angular應用程序的Config中調用adalAuthenticationService.init()時需要設置實例,租戶,clientId和端點成員。 有關端點示例,請參見示例2,但將GUID替換為URL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM