当前位置: 首页 > 编程语言 > C#

如何从On Premise SharePoint 2013和ADFS获取FedAuth Cookie分享

时间:2023-04-10 20:16:23 C#

C#学习教程:如何从OnPremiseSharePoint2013和ADFS获取FedAuthCookie可以使用ADFS作为声明提供程序成功登录到Sharepoint2013。现在我尝试从我的C#应用程序登录到Sharepoint。我可以使用以下命令从adfs请求saml断言令牌。现在,我想帮助将saml令牌发布到SharePoint并检索FedAuthcookie,这样我就可以被动地登录到SharePoint2013并从C#应用程序上传文档。当我调用最后一个方法时,PostSharePointSTS()没有设置cookie。大部分代码都是LeandroBoffi的帮助[TestMethod]publicvoidGetSamlTestMethod(){varclient=newWebClient();client.Headers.Add("Content-Type","application/soap+xml;charset=utf-8");stringusername="Administrator@2012r2.local";字符串密码=“密码1”;stringadfsServer="https://logon.2012r2.local/adfs/services/trust/2005/UsernameMixed";stringsharepoint="https://portal.2012r2.local/_trust/";varsamlRequest=GetSAML().Replace("[用户名]",用户名).Replace("[密码]",密码).Replace("[To]",adfsServer).Replace("[applyTo]",sharepoint);varresult=client.UploadString(地址:“https://logon.2012r2.local/adfs/services/trust/2005/UsernameMixed”,方法:“POST”,数据:samlRequest);PostSharePointSTS(GetSAMLAssertion(结果));}privatestaticstringGetSAMLAssertion(stringresponse){XDocumentsamlResponse=XDocument.Parse(response);//检查响应xml是否有故障/错误if(samlResponse.Root==null)thrownewApplicationException("从身份验证服务收到无效响应。");XNamespaces="http://www.w3.org/2003/05/soap-envelope";XNamespacepsf="http://schemas.microsoft.com/Passport/SoapServices/SOAPFault";XNamespacewst="http://schemas.xmlsoap.org/ws/2005/02/trust";//"http://docs.oasis-open.org/ws-sx/ws-trust/200512";//XNamespacewsp="http://schemas.xmlsoap.org/ws/2004/09/policy";XNamespacewsa="http://www.w3.org/2005/08/addressing";XNamespacewsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";conststringsaml="urn:oasis:names:tc:SAML:1.0:assertion";//登录令牌位于消息正文的SAML断言元素中XDocumentxDoc=XDocument.Parse(response,LoadOptions.PreserveWhitespace);varassertion=fromeinxDoc.Descendants()wheree.Name==XName.Get("Assertion",saml)selecte;字符串samlAssertion=assertion.FirstOrDefault().ToString();//由于某种原因断言字符串需要加载到XDocument中//并写出以使XML有效。否则我们会从ADFSXDocumentdoc1=XDocument.Parse(samlAssertion);返回无效的//XML错误;samlAssertion=doc1.ToString(SaveOptions.DisableFormatting);返回samlAssertion;}privatestaticstringGetSAML(){conststringsaml=@"http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issuehttp://www.w3.org/2005/08/addressing/匿名[收件人][用户名][密码][申请]http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKeyhttp://schemas.xmlsoap.org/ws/2005/02/trust/问题urn:oasis:names:tc:SAML:1.0:assertion";返回saml;}privatestaticvoidPostSharePointSTS(stringassertion){//将BinarySecurityToken提交给SPO并检索响应varloginUri=newUri("https://logon.2012r2.local/adfs/ls?wa=wsignin1.0&wtrealm=urn:sharepoint:门户网站”);varrequestCookies=newCookieContainer();var请求=(HttpWebRequest)WebRequest.Create(loginUri);request.AllowAutoRedirect=false;request.ContentType="application/x-www-form-urlencoded";request.ContentLength=assertion.Length;request.CookieContainer=requestCookies;request.Method="POST";request.UserAgent="Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Win64;x64;Trident/5.0)";使用(varrequestWriter=newStreamWriter(request.GetRequestStream())){requestWriter.Write(断言);requestWriter.Close();}varresponse=(HttpWebResponse)request.GetResponse();开关(response.StatusCode){caseHttpStatusCode.OK:caseHttpStatusCode.Found:break;//TODO:记录错误?//默认://返回假;当我尝试将给定的SAML令牌发布到SharePOint时,我得到以下信息但没有设置cookie。HTTP/1.1302找到内容长度:0内容类型:文本/html;charset=utf-8位置:https://logon.2012r2.local:443/adfs/ls/wia?wa=wsignin1.0&wtrealm=urn:sharepoint:portal服务器:Microsoft-HTTPAPI/2.0日期:2014年8月16日星期六格林威治标准时间10:55:51此响应未设置任何cookie。此响应不包含P3P标头。验证P3P策略:http://www.w3.org/P3P/validator.html了解更多信息:http://fiddler2.com/r/?p3pinfo为什么不使用标准SharePointCSOM库来做任何事情在SharePoint中?CSOM代表SharePoint端的用户自动执行所有必要的ADFS交互。以上就是C#学习教程:如何从OnPremiseSharePoint2013和ADFS获取FedAuthCookie共享的所有内容。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: