简体   繁体   English

SharePoint 连接仅在运行 Fiddler 时有效?

[英]SharePoint Connection Only Works When Running Fiddler?

I'm trying to get the basics down on connecting to SharePoint Online (as opposed to an on-prem SharePoint) using C# in my Unity application.我正在尝试在我的 Unity 应用程序中使用 C# 连接到 SharePoint Online(而不是本地 SharePoint)的基础知识。 Below is the code I am using as a starting point (more handling/security to be involved later).下面是我用作起点的代码(稍后将涉及更多处理/安全性)。 What is mind-blowing is that this code will work if I have Fiddler running (a web-activity snooper tool) or if I compile it outside of Unity.令人兴奋的是,如果我运行 Fiddler(一个网络活动窥探工具)或者如果我在 Unity 之外编译它,那么这段代码就可以工作。 Without Fiddler (and in Unity), I get the error: SocketException: No connection could be made because the target machine actively refused it.如果没有 Fiddler(在 Unity 中),我会收到错误消息: SocketException: No connection could be made because the target machine actively refused it. What could Fiddler possibly be doing to make this work, and is there a way I can duplicate it in my code? Fiddler 可能会做些什么来完成这项工作,有没有办法可以在我的代码中复制它?

    public void SharepointLogin()
    {
        //string url = "https://<private_server>/sites/sitename";
        string url = "https://<company>.sharepoint.com/sites/otherSiteName";
        //string folderpath = "/sites/sitename/folder/";
        string folderpath = "/sites/otherSiteName/folder/";
        string filepath = "W:/ServerData.csv";

        using (var ctx = new ClientContext(url))
        {
            ctx.AuthenticationMode = ClientAuthenticationMode.Default;
            SecureString ss = new NetworkCredential("", password).SecurePassword;

            //ctx.Credentials = new System.Net.NetworkCredential(username, password);
            ctx.Credentials = new SharePointOnlineCredentials(usernameWithDomain, ss);
            
            var filename = Path.GetFileName(filepath);
            using (FileStream fs = new FileStream(filepath, FileMode.Open))
            {
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, folderpath + filename, fs, true);
            }
        }

    }

Additional info:附加信息:

Semi-related but maybe insightful, Unity seems to have trouble using CredentialCache.DefaultNetworkCredentials (it actually appears not to include it at all) when connecting to an "on-prem" SharePoint site which forces me to re-create them with System.Net.NetworkCredentials instead.半相关但可能很有见地,当连接到“本地”SharePoint 站点时,Unity 似乎无法使用CredentialCache.DefaultNetworkCredentials (它实际上似乎根本不包括它),这迫使我用System.Net.NetworkCredentials重新创建它们System.Net.NetworkCredentials代替。 (see commented lines which DOES work for the other site). (请参阅确实适用于其他站点的注释行)。 If I build that same code outside Unity, it also works fine so clearly Unity is stripping it for some reason.如果我在 Unity 之外构建相同的代码,它也可以正常工作,很明显 Unity 出于某种原因正在剥离它。 Unity also seems to have an issue with our proxy in general, but since I can get through with the on-prem server, I'm hoping there's also a way to get through to the SharePoint Online server--especially since Fiddler somehow makes it work and thus Unity must not simply be stripping the credentials like it did with DefaultCredentials. Unity 的代理似乎也有一般问题,但由于我可以通过本地服务器,我希望还有一种方法可以通过 SharePoint 在线服务器 - 特别是因为 Fiddler 以某种方式做到了工作,因此 Unity 不能像使用 DefaultCredentials 那样简单地剥离凭据。

SharePoint Online requires a SharePointOnlineCredentials object specifically so I don't get the same workaround as with on-prem. SharePoint Online 特别需要 SharePointOnlineCredentials object,所以我没有得到与本地相同的解决方法。 Fiddler did not fix the on-prem connection, however, when using DefaultNetworkCredentials like it seems to when using the SharePointOnlineCredentials, so clearly it's a different failure mechanism. Fiddler 没有修复本地连接,但是,在使用 DefaultNetworkCredentials 时,就像在使用 SharePointOnlineCredentials 时一样,因此显然这是一种不同的故障机制。 I've also read through https://www.telerik.com/blogs/help!-running-fiddler-fixes-my-app- but it does not appear to provide my answer.我也通读了https://www.telerik.com/blogs/help!-running-fiddler-fixes-my-app-但它似乎没有提供我的答案。

Unity itself was failing due to proxy issues.由于代理问题,Unity 本身失败了。 With Fiddler running, it overrides the proxy and reroutes connections through itself.在 Fiddler 运行时,它会覆盖代理并通过自身重新路由连接。 Unity has no issues getting to the Fiddler overridden proxy and then fiddler uses the AutoProxy (PAC) script which Unity could not do. Unity 可以毫无问题地访问 Fiddler 覆盖的代理,然后 fiddler 使用 Unity 无法执行的 AutoProxy (PAC) 脚本。 This completed the connection for my SharePoint access.这完成了我的 SharePoint 访问的连接。 Since SharePoint Online uses different web addresses than our internal on-prem SharePoint sites, the proxy acts differently for those requests and hence the difference in success with/without Fiddler.由于 SharePoint Online 使用不同的 web 地址而不是我们的内部本地 SharePoint 站点,因此代理对这些请求的行为不同,因此在没有 Fiddler 的情况下成功的差异。

Note: No clue why it doesn't work when Unity compiles the code as one might expect the compiled code to no longer be hindered by Unity's limitations, but apparently Unity bakes the failure into it whereas Visual Studio seems to work fine if compiling outside Unity.注意:不知道为什么当 Unity 编译代码时它不起作用,因为人们可能期望编译的代码不再受到 Unity 限制的阻碍,但显然 Unity 将失败融入其中,而 Visual Studio 似乎在 Unity 外部编译时工作正常.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM