冒充C#,asp.net,windows身份

时间:2016-04-21 07:40:25

标签: c# asp.net tfs asp.net-identity impersonation

所以我正在尝试开发一个Web应用程序(c#,asp.net 4.5),它使用Windows身份和模拟连接到TFS(团队基础服务器)并检索一些元素(工作项,任务等)。问题是模拟只能在运行应用程序的服务器上运行,就像我的身份一样。每当其他人尝试连接时,他们的访问被拒绝,应用程序崩溃未经授权的错误“TF30063:您无权访问[服务器地址]”。当然其他人是授权的,因为他们可以直接访问TFS(不是通过我的应用程序),其中一些是管理员。

返回错误的代码段是

 protected TfsTeamProjectCollection TeamProjectCollection
        {
            get
            {
                if (tpc == null)
                {
                  // I HAVE ALSO TRIED THIS COMMENTED OUT PART, TOO. Still doesn't work.
                  //  using (WindowsIdentity.GetCurrent().Impersonate())
                  //  {
                  //    tpc = new TfsTeamProjectCollection(new Uri(ConnectionString));
                  //  }
                        var identityDescriptor = Microsoft.TeamFoundation.Framework.Client.IdentityHelper.CreateDescriptorFromSid(WindowsIdentity.GetCurrent().User);
                        tpc = new TfsTeamProjectCollection(new Uri(ConnectionString), identityDescriptor);
                }
                return tpc;
            }
        }

之前还有其他人遇到过这个吗?我花了几天时间研究网络,但没有找到有效的答案。希望你们能帮忙! 其他提到:在web.config中我设置了“identity impersonate = true”,在iis我启用了asp.net模拟和Windows身份验证。所有其他auth选项都被禁用。

2 个答案:

答案 0 :(得分:0)

我使用这段代码模拟WCF服务:

/// <summary>
/// Impersonation - Creates new instance of TfsTeamProjectCollection object using a different user;
/// </summary>
/// <param name="serverUri">Tfs server uri you want to connect using Impersonation</param>
/// <param name="userToImpersonate">Account name of the user you want to Impersonate</param>
private void Impersonation(Uri serverUri, string userToImpersonate)
{
    try
    {
        eventLog1.WriteEntry("Start Impersonation");
        // Read out the identity of the user we want to impersonate
        TeamFoundationIdentity identity = ims.ReadIdentity(IdentitySearchFactor.AccountName,
            userToImpersonate,
            MembershipQuery.None,
            ReadIdentityOptions.None);
        eventLog1.WriteEntry(identity.DisplayName);
        this.impersonatedTFSUser = new TfsTeamProjectCollection(serverUri, identity.Descriptor);
        eventLog1.WriteEntry(impersonatedTFSUser.Name);
        eventLog1.WriteEntry("End Impersonation");
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

答案 1 :(得分:0)

在一些帮助下解决了这个问题。所以事实是,如果我使用TFS模拟,应用程序池需要与可以访问TFS的用户一起运行,而不是更多。我禁用了asp.net模拟,只启用了Windows Auth。此外,我将“身份”从应用程序池高级设置更改为我的TFS用户。 现在还有另一个问题。使用该Web应用程序的任何人只能看到他和我的用户都有权使用的项目。此外,在保存工作项时,TFS使用应用程序池标识(在本例中为我的用户)和运行应用程序的用户的“上次修改”自动填充“created by”字段。这不是我希望它如何工作,所以我将做更多的研究。

相关问题