SharePoint 连接仅在运行 Fiddler 时有效? [英] SharePoint Connection Only Works When Running Fiddler?

查看:80
本文介绍了SharePoint 连接仅在运行 Fiddler 时有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Unity 应用程序中使用 C# 了解连接 SharePoint Online(而不是本地 SharePoint)的基础知识.下面是我用作起点的代码(稍后将涉及更多处理/安全性).令人兴奋的是,如果我让 Fiddler 运行(一个网络活动窥探工具)或者如果我在 Unity 之外编译它,这段代码将起作用.如果没有 Fiddler(在 Unity 中),我会收到错误:SocketException: 由于目标机器主动拒绝它,因此无法建立连接. 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. 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. Without Fiddler (and in Unity), I get the error: 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?

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

    }

附加信息:

半相关但可能有见地,当连接到本地"时,Unity 似乎无法使用 CredentialCache.DefaultNetworkCredentials(实际上似乎根本不包含它).SharePoint 网站迫使我使用 System.Net.NetworkCredentials 重新创建它们.(请参阅对其他站点有效的注释行).如果我在 Unity 之外构建相同的代码,它也可以正常工作,很明显 Unity 出于某种原因正在剥离它.Unity 的代理似乎也有一般问题,但由于我可以通过本地服务器,我希望还有一种方法可以访问 SharePoint Online 服务器——尤其是因为 Fiddler 以某种方式实现了它工作,因此 Unity 不能像使用 DefaultCredentials 那样简单地剥离凭据.

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. (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 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.

SharePoint Online 特别需要一个 SharePointOnlineCredentials 对象,因此我无法获得与本地相同的解决方法.Fiddler 没有修复本地连接,但是,当使用 DefaultNetworkCredentials 时,就像使用 SharePointOnlineCredentials 时一样,很明显,这是一种不同的故障机制.我还通读了 https://www.telerik.com/blogs/help!-running-fiddler-fixes-my-app- 但它似乎没有提供我的答案.

SharePoint Online requires a SharePointOnlineCredentials object specifically so I don't get the same workaround as with on-prem. 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. 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.

推荐答案

Unity 本身由于代理问题而失败.在 Fiddler 运行时,它会覆盖代理并通过自身重新路由连接.Unity 访问 Fiddler 覆盖的代理没有问题,然后 fiddler 使用 Unity 无法执行的 AutoProxy (PAC) 脚本.这完成了我的 SharePoint 访问的连接.由于 SharePoint Online 使用的网址与我们内部的内部部署 SharePoint 网站不同,因此代理对这些请求的行为不同,因此使用/不使用 Fiddler 的成功率也不同.

Unity itself was failing due to proxy issues. With Fiddler running, it overrides the proxy and reroutes connections through itself. Unity has no issues getting to the Fiddler overridden proxy and then fiddler uses the AutoProxy (PAC) script which Unity could not do. This completed the connection for my SharePoint access. 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.

注意:不知道为什么在 Unity 编译代码时它不起作用,因为人们可能期望编译后的代码不再受到 Unity 限制的阻碍,但显然 Unity 将失败融入其中,而 Visual Studio 似乎可以正常工作,如果在 Unity 之外编译.

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.

这篇关于SharePoint 连接仅在运行 Fiddler 时有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆