Write in front
Because the site on the SharePoint server is logged on as a domain user Windows authentication, and the app project can provide a username and password, the customer is reluctant to transfer this on the network. So it provides a way to use SSL certificate authentication. The combination of Webhttprequest and SharePoint's client object model is pretty reliable, and here are two ways to upload a file with a certificate sending request.
Core code
1 /// <summary>2 ///Uploading Files3 /// </summary>4 /// <param name= "Serverreleativeurl" >relative path of folder</param>5 /// <param name= "FileName" ></param>6 /// <returns></returns>7 Public stringUploadFile (stringServerreleativeurl,stringFileName,byte[] Data,BOOLboverwrite)8 {9 if(Data. Length = =0)Ten { One return string. Empty.toerrorjson (" -","do not allow empty files to be uploaded"); A } - if(string. IsNullOrEmpty ( This. _siteinfo.username)) - { the return string. Empty.toerrorjson (" -",NewArgumentNullException ("User Name parameter is empty"). Message); - } -ClientContext SpContext =NewClientContext ( This. _siteinfo.appurl); - //spcontext.credentials = new NetworkCredential (This._username, This._pwd, this.strdomain); + /* - * SSL Support + * SSL authentication protocol relies on certificates : A The support for SSL in the * NET Framework consists of two parts. at * SSL on HTTP is a special case (but most widely used) implemented by the HttpWebRequest class, which is ultimately available to the WEB service client proxy. - * To enable SSL, you do not have to perform any special operations except to specify a URL that uses the Https: protocol. - * When connecting to an SSL-protected endpoint, the server certificate is validated on the client. - * If validation fails, the connection is immediately closed based on the default settings. You can override this behavior by recalling a class named ServicePointManager. - * Each time the HTTP client's stack is verified by the certificate, it will first check if it can be recalled; - * If possible, execute your code. To hook up the callback, you must provide a delegate of type Remotecertificatevalidationcallback: in */ -Servicepointmanager.servercertificatevalidationcallback =NewRemotecertificatevalidationcallback (verifyservercertificate); to +Spcontext.executingwebrequest + =spcontext_executingwebrequest; -WEB website =Spcontext.web; theFolder folder =website. Getfolderbyserverrelativeurl (Serverreleativeurl); * Try $ {Panax Notoginsengfilecreationinformation file =Newfilecreationinformation (); -File. Content =data; theFile. URL =FileName; +File. Overwrite =Boverwrite; A folder. Files.add (file); the spcontext.executequery (); + } - Catch(Exception ex) $ { $ Throwex; - } - the return string. Empty.tosuccessjson (" $","Upload Successful"); - }Wuyi /// <summary> the ///in the callback, you get the server certificate, an error code, and an incoming chain object, and then you can perform your own check and return True or false. - ///If a condition such as a certificate has expired during development or testing is present, it is beneficial to close one of the checks. On the other hand, doing so can also perform a more rigorous validation strategy than the default. Wu /// https://msdn.microsoft.com/zh-cn/magazine/cc163454.aspx#S6 - /// </summary> About /// <param name= "Sender" ></param> $ /// <param name= "certificate" ></param> - /// <param name= "Chain" ></param> - /// <param name= "sslpolicyerrors" ></param> - /// <returns></returns> A Private BOOLVerifyservercertificate (Objectsender, X509Certificate certificate, + x509chain chain, sslpolicyerrors sslpolicyerrors) the { - if(sslpolicyerrors = = Sslpolicyerrors.none)return true; $ the foreach(X509chainstatus sinchchain. Chainstatus) the { the //allows expired certificates the if(string. Equals (S.status.tostring (),"Nottimevalid", - stringcomparison.ordinalignorecase)) in return true; the } the About return false; the } the voidSpcontext_executingwebrequest (Objectsender, Webrequesteventargs e) the { +HttpWebRequest Webreq =e.webrequestexecutor.webrequest; - #regionThis way the individual can upload, the team can not upload the //string pfxpath = configurationmanager.appsettings["Clientsigningcertificatepath"];Bayi //string pfxpassword = configurationmanager.appsettings["Clientsigningcertificatepassword"]; the //X509Certificate2 cert = new X509Certificate2 (Pfxpath, pfxpassword); the //webreq.credentials = new NetworkCredential (This._username, _pwd, strdomain); - //WEBREQ.CLIENTCERTIFICATES.ADD (cert); - #endregion the checkappexsit (); the varAccesstoken = Tokenhelper.gets2saccesstokenwithwindowsidentity (NewUri (_siteinfo.appurl),NULL); theWebreq.method ="Post"; theWebreq.accept ="Application/json;odata=verbose"; -WEBREQ.HEADERS.ADD ("Authorization","Bearer"+accesstoken); the}
In this way, certificates can be used directly for authentication without providing account information. Here are some ideas and hope to help you.
Summarize
Uploading files in SharePoint, using the client object model is a simpler way of comparing the rest API, using the rest API, I did not succeed, do not know that the place is a problem, it is a headache, finally no way to adopt this way.
Reference article:
http://blog.csdn.net/starlee/article/details/4441150
Https://msdn.microsoft.com/zh-cn/magazine/cc163454.aspx#S6
Http://www.cnblogs.com/dosboy/p/4045773.html
Webhttprequest for use in SharePoint document libraries