【问题标题】:Unable to cast object of type 'System.Security.Claims.ClaimsIdentity' to type 'System.Security.Principal.WindowsIdentity'无法将“System.Security.Claims.ClaimsIdentity”类型的对象转换为“System.Security.Principal.WindowsIdentity”类型
【发布时间】:2015-06-24 09:58:16
【问题描述】:

我尝试在本地运行 3 个应用程序。

在第一个上,我正在运行集成测试,该测试正在调用第二个(“中间”)应用程序,该应用程序应该在其本地系统上创建报价,然后调用第三个系统,即负责用户管理的公司“全球”系统。

System.Security.Claims.ClaimsIdentity 转换为System.Security.Principal.WindowsIdentity 时失败

protected ApplicationAuthorisationService()
{
    CurrentIdentity = (WindowsIdentity)Thread.CurrentPrincipal.Identity;
}

我对“负责用户管理的全球系统”知之甚少,因为它正在由其他部门处理,我仍然需要调查为什么我的本地计算机上的测试失败。

'middle' 应用程序正在构造 url 并简单地通过 GET 调用 global

private HttpClient GetHttpClient()
{
    try
    {
        var client = new HttpClient
        {
            BaseAddress = this.BaseUri,
        };

        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        return client;
    }
    catch (Exception ex)
    {
        ILogHelper.LogException(ex, Log, LoggerLevel.Error);
        throw;
    }
}

进行实际调用的片段。

using (var client = this.GetHttpClient())
{
    client.SetBearerToken(this._token.AccessToken);
    var uri = new Uri(client.BaseAddress, string.Format("api/crmcustomer/?FirstName={0}&Surname={1}&DateOfBirth={2}&PostCode={3}&GetLatestWhenThereAreMultipleResults={4}",
        parameter.FirstName,
        parameter.Surname,
        parameter.DateOfBirth.HasValue ? parameter.DateOfBirth.Value.ToString("yyyy-MM-dd") : string.Empty,
        parameter.PostCode,
        parameter.GetLatestWhenThereAreMultipleResults));

    this.Log.Info("URI: " + uri);

    this.Log.Debug("parameter.FirstName: " + parameter.FirstName);
    this.Log.Debug("parameter.Surname: " + parameter.Surname);
    if (parameter.DateOfBirth != null)
        this.Log.Debug("parameter.DateOfBirth: " + parameter.DateOfBirth.Value.ToString("yyyy-MM-dd"));
    this.Log.Debug("parameter.PostCode: " + parameter.PostCode);
    this.Log.Debug("parameter.GetLatestWhenThereAreMultipleResults: " + parameter.GetLatestWhenThereAreMultipleResults);
    var response = await client.GetAsync(uri).ConfigureAwait(false);
    response.EnsureSuccessStatusCode();
    return await response.Content.ReadAsAsync<CtiResponse>();
}

这可能是什么原因?

【问题讨论】:

  • “所以这个异常没有什么意义” - 如果继承是相反的,那么这个异常就没有意义了。苹果 (WindowsIdentity) 是水果 (ClaimsIdentity) 的一种。这并不意味着你可以随便拿一块水果,然后神奇地把它变成一个苹果。
  • @Damien_The_Unbeliever 我刚刚经历过这样一个时刻,我怀疑自己是愚蠢还是盲目。谢谢。

标签: c# .net security casting


【解决方案1】:

对于那些想知道的人。

问题是项目的版本是set to .NET 4.0 while这个继承get's implemented on .NET 4.5

【讨论】:

    猜你喜欢
    • 2015-05-11
    • 1970-01-01
    • 2021-12-06
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    相关资源
    最近更新 更多