【问题标题】:Calling a Web API hosted on the same server from a controller, the AD name for auth is the IIS app pool name not the user name从控制器调用托管在同一服务器上的 Web API,auth 的 AD 名称是 IIS 应用程序池名称而不是用户名
【发布时间】:2020-09-10 17:49:53
【问题描述】:

应用说明

该应用程序包含两个功能。在控制器内是对 Web API 的调用,如下所示:

    private async Task<string> CallMciPostsApi()
    {
        using (var handler = new HttpClientHandler { UseDefaultCredentials = true })
        using (var client = new HttpClient(handler))
        {
            var response = await 
                client.GetAsync("https://medapp10.med.state.sbu/emed2webapi/api/mcipost");
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }
            else
            {
                EventLogger.Info(response.ReasonPhrase);
            }
        }
        return null;
    }

控制器有 [Authorize(Roles = ("MED Supervisors Overseas"))]

在剃须刀页面内进行 Ajax 调用:

function getCountries() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'https://medapp10.med.state.sbu/Emed2webapi/api/mcipost');
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.onload = function () {
        loadCountrySelect(JSON.parse(xhr.responseText), true);
    }
    xhr.send();
}

web.config

<authorization>
  <allow users="*" />
  <deny users="?" />
</authorization>
<identity impersonate="false" />

网络 API

    [Authorize(Roles = ("MED Supervisors Overseas"))]
[RoutePrefix("api/mcipost")]
public class MciPostController : BaseController
{
    /// <summary>
    /// Status Return a list of MciPost objects
    /// </summary>
    /// <returns></returns>
    [Route()]
    [HttpGet]
    public virtual IHttpActionResult Get()
    {
        try
        {
            var entityList = MCIPostRepository.Get();

            if (entityList == null)
            {
                return InternalServerError();   // 500 Internal Server Error
            }

            if (entityList.Count == 0)
            {
                return NotFound();
            }

            return Ok(entityList); // 200 OK
        }
        catch (Exception ex)
        {
            EventLogger.Error(ex);
        }
    }
}

问题

  1. 安装 Web API 并在 Visual 下运行应用程序 Studio - IIS Express,AJAX 调用失败但控制器调用 成功。 Visual Studio 以本地管理员身份运行 基于运行后构建脚本和无法查看网络资源的问题,VS 以管理员身份运行这一事实可能很重要。 em>

  2. 但是,如果应用程序安装在 Web API 主机服务器上, 发生相反的情况,AJAX 调用成功,控制器 调用失败(response.ReasonPhrase="UnAuthorized")。

经过多天的研究和与同事的咨询,为什么会这样仍然是个谜。这是我第一次处理 Active Directory,我怀疑我遗漏了一些对于更广泛地使用它的人来说显而易见的东西。

关于问题 2,创建了一个自定义的授权属性来记录授权错误。活动目录名称是“IIS APPPOOL”名称。

  1. 尝试在 web.config 中设置 impersonate="true"

**

【问题讨论】:

标签: c# asp.net-mvc api active-directory


【解决方案1】:

来自: Web api authorization by capturing client's service account name

将 HttpClient 更改为 WebClient (UseDefaultCredentials=true;) 并在 web.config 上设置 impersonate=true。

【讨论】:

    猜你喜欢
    • 2014-08-23
    • 2018-08-25
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2017-03-02
    • 2019-01-18
    • 2014-10-01
    • 1970-01-01
    相关资源
    最近更新 更多