通过ADFS将域用户身份验证到SharePoint Online

时间:2016-12-03 13:30:22

标签: c# authentication sharepoint office365 adfs

我需要编写一个C#应用程序,它将连接到SharePoint Online文档库,并允许用户从库中导入内容(文档库正在替换旧的网络驱动器)。我们的Office 365租赁使用内部部署ADFS服务器(完全联合身份)提供的SSO进行身份验证。

我在想,为了连接到文档库,我需要从ADFS服务器请求某种授权令牌,以便在连接到doc库时传递到SharePoint Online的登录URL。我以前从来没有做过这样的事情,所以我想知道这里有没有人做过类似的事,或者有什么好的提示从哪里开始?该应用程序仅在加入域的计算机上运行,​​因此不需要考虑网络外场景。

非常感谢任何建议!

2 个答案:

答案 0 :(得分:0)

您的应用程序需要的是o365会话cookie,因此Sharepoint允许您使用文档库。

你应该看看这里: Remote Authentication in SharePoint Online Using Claims-Based Authentication

基本上,我们的想法是在您的应用程序中打开隐藏的Web浏览器。您调用Sharepoint站点,o365重定向到您的ADFS进行身份验证,blablabla将进行标准身份验证过程。

身份验证过程完成后,Sharepoint会发出一个名为" FedAuth"的会话cookie。 (onprem,也许它在o365中有另一个名字)。您从Web浏览器中提取此cookie并使用它来呼叫您的Sharepoint在线站点(一旦您拥有cookie,您就可以" Dispose" Web浏览器)。

答案 1 :(得分:0)

我设法让ADFS使用以下代码发出令牌:

    public GenericXmlSecurityToken GetToken()
    {
        WS2007HttpBinding binding = new WS2007HttpBinding(SecurityMode.Transport);
        binding.Security.Message.EstablishSecurityContext = false;
        binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
        WSTrustChannelFactory factory = new WSTrustChannelFactory((binding), new EndpointAddress(stsEndpoint));
        factory.TrustVersion = TrustVersion.WSTrustFeb2005;
        factory.Credentials.SupportInteractive = false;
        var rst = new RequestSecurityToken
        {
            RequestType = RequestTypes.Issue,
            AppliesTo = new EndpointReference(realm),
            KeyType = KeyTypes.Bearer
        };
        IWSTrustChannelContract channel = factory.CreateChannel();
        return channel.Issue(rst) as GenericXmlSecurityToken;
    }

然后,这用于参数化我提交给MS STS服务器进行身份验证的SOAP请求。完成后,我可以使用HttpWebRequest和CookieContainer从SharePoint获取cookie,然后我使用此代码

    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool InternetSetCookie(string url, string name, string data);

公开InternetSetCookie方法以保存用户会话的cookie。

相关问题