I have an application hosted on one server that's not a part of my SharePoint farm. I need it to to post a new list item to a SharePoint List. The Default SharePoint Zone (where it will be posted to) is configured to use Claims Authentication, Integrated Windows Authentication.
The identity that the application is running under has full administrative access over the list that I'm trying to post to. Every time I post I get a "403 forbidden" error. If I remove the endPointRequest.credentials line I get a 401 error.
var data = "{'__metadata':{'type':'SP.Data.SpecialListItem'}, 'Special':'" + txtSpecial + "'}";
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("http://portal/_api/web/List/GetByTitle('Special')");
endpointRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
endpointRequest.Method = "POST";
endpointRequest.Accept = "application/json;odata=verbose";
endpointRequest.ContentType = "application/json;odata=verbose";
endpointRequest.ContentLength = data.Length;
StreamWriter writer = new StreamWriter(endpointRequest.GetRequestStream());
writer.Write(data);
writer.Flush();
using (HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(endpointResponse.GetResponseStream()))
{
string result = reader.ReadToEnd();
}
}
Related
We have a website. We usually use Microsoft forefront for asking the user for its username and password to log in. Recently, we have a requirement to allow users to use Office365 to log in to our website. So we have to integrate Office365 into our website to allows a user to login into our website using Office365. So I am trying to use Microsoft.Graph. I have registered my app as web and got the client ID and well as a secret key. I am using this information to make a call to Microsoft Graph.
I have tried to call API as shown below. My first request(request1) is successful by second request (request2) throws an error saying its a bad request
"The remote server returned an error: (400) Bad Request." System.Net.WebException**
var request1 = (HttpWebRequest)WebRequest.Create("https://login.microsoftonline.com/{tenant}/adminconsent?client_id=myclientidvalue&state=12345&redirect_uri=http://localhost:36541/");
var response1 = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response1.GetResponseStream()).ReadToEnd();
var request2 = (HttpWebRequest)WebRequest.Create("https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token");
var postData = "client_id=myclientid";
postData += "&scope=http://localhost:36541/.default";
postData += "&client_secret=mysecretkey";
postData += "&grant_type=client_credentials";
var data = Encoding.ASCII.GetBytes(postData);
request2.Method = "POST";
request2.ContentType = "application/x-www-form-urlencoded";
request2.ContentLength = data.Length;
request1.Host = "login.microsoftonline.com";
using (var stream = request2.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response2 = (HttpWebResponse)request2.GetResponse();
var responseString1 = new StreamReader(response2.GetResponseStream()).ReadToEnd();
I'm working with a third party that refuses to fix their scheme for a published WSDL. The issue is the SOAP service is expecting different name spaces than the WSDL is providing. So in my C# app I'm having a lot of trouble using the Proxy by .Net. In an attempt to work around this I want to just send a web-request and package up my data
I just can't figure out how to add my signature to the header of my request.
Microsoft.Web.Services3.SoapEnvelope soapEnvelopeXml = new Microsoft.Web.Services3.SoapEnvelope();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(#"https://Illinois-stage.tylerhost.net/efm/FilingReviewMDEPort.svc");
request.Headers.Add("SOAPAction:\"urn:oasis:names:tc:legalxml-courtfiling:wsdl:WebServicesProfile-Definitions-4.0/FilingReviewMDEPort/ReviewFilingRequest\"");
request.Headers.Add("Accept-Encoding: gzip, deflate");
request.KeepAlive = true;
request.Headers.Add("MIME-Version: 1.0");
request.ContentType = "multipart/related; type=\"application/xop+xml\";start=\"<http://tempuri.org/0>\";boundary=\"uuid:936f2c7e-590a-4f19-b154-ce8285adf18a+id=2\";start-info=\"text/xml\"";
request.Method = "POST";
soapEnvelopeXml.Load(#"c:\temp\ReviewFilingRequest.xml");
System.IdentityModel.Tokens.X509SecurityToken securityToken = new System.IdentityModel.Tokens.X509SecurityToken(X509);
Microsoft.Web.Services3.Security.Tokens.X509SecurityToken signatureToken = new Microsoft.Web.Services3.Security.Tokens.X509SecurityToken(X509);
MessageSignature sig = new MessageSignature(signatureToken);
////////// XmlDocumentFragment xfrag = soapEnvelopeXml.CreateDocumentFragment();
//////////xfrag.InnerXml = messageHeader;
//////////soapEnvelopeXml.DocumentElement.FirstChild.AppendChild(xfrag);
XmlDocument xmlDoc = new XmlDocument();
MemoryStream xmlStream = new MemoryStream();
xmlDoc.Save(xmlStream);
using (var writer = XmlDictionaryWriter.CreateMtomWriter(xmlStream, Encoding.UTF8, int.MaxValue, "text/xml", "uuid:936f2c7e-590a-4f19-b154-ce8285adf18a+id=2", "http://tempuri.org/0",false,false))
{
soapEnvelopeXml.WriteTo(writer);
using (Stream stream = request.GetRequestStream())
{
stream.Write(xmlStream.ToArray(),0, xmlStream.ToArray().Length );
}
}
using (WebResponse response = request.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
Console.WriteLine(soapResult);
}
}
A couple things that might be able to help.
What error message are you getting back? Access Denied (or something along those lines)?
Have you tried looking at which headers you are missing? The best way to do this is to download WireShark or Telerik's Fiddler which allows HTTPS decryption. Using one of these, you can access the website in an internet browser and view the headers that are being sent through a normal browser.
I ended up signing it in Node.js Making the switch to node gave me a lot more control over the message. As an added bonus I didn't have to pay the Microsoft Tax.
https://www.npmjs.com/search?q=soap&page=1&ranking=optimal
I am trying to read the Out Of Office status and message from other users in the organization from my c# program. We are running Exchange 2013 on premises.
This app is running as an Active Directory account (with it's own exchange mailbox), and I cannot use impersonation.
I have spent some time trying out the solutions to similar questions such as:
Retrieve out of office Status using EWS 2.0 This one uses impersonation
Read out of office from exchange server for other users I'm not using the Outlook Object Model
How to get Out of Office for another mailbox again no impersonation for me
http://gsexdev.blogspot.com/2011/11/using-mailtips-in-ews-to-get-oof-out-of.html I don't have reference to ExchangeServiceBinding, even though I'm using Microsoft.Exchange.WebServices.Data;
http://blogs.msdn.com/b/devmsg/archive/2014/06/03/ews-how-to-retrieve-the-oof-out-of-facility-settings-message-using-ews-for-an-exchange-user.aspx This one takes as a paramater, urlname and I'm not sure where that url is coming from. This one seems the most promising though, any ideas where that comes from?
I'm trying to get something like:
public void checkOOF(string userEmail){
bool isOOF = checkstuff(userEmail);
string message;
if(isOOF)
message = getOOFMessage(userEmail);
}
Please help me understand, thank you.
This is what I ended up using and it works.
public static string getOOM(string emailToCheck) //needs to be full email of user.
{
string EWSurl = String.Format("https://{0}/EWS/Exchange.asmx", ExchangePath);
WebRequest webRequest = WebRequest.Create(EWSurl);
HttpWebRequest httpwebRequest = (HttpWebRequest)webRequest;
httpwebRequest.Method = "POST";
httpwebRequest.ContentType = "text/xml; charset=utf-8";
httpwebRequest.ProtocolVersion = HttpVersion.Version11;
httpwebRequest.Credentials = new NetworkCredential("user", "password", "domain");//service Account
httpwebRequest.Timeout = 60000;
Stream requestStream = httpwebRequest.GetRequestStream();
StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
StringBuilder getMailTipsSoapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
getMailTipsSoapRequest.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
getMailTipsSoapRequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
getMailTipsSoapRequest.Append("xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\"><soap:Header>");
getMailTipsSoapRequest.Append(" <t:RequestServerVersion Version=\"Exchange2010\"/></soap:Header><soap:Body>");
getMailTipsSoapRequest.Append("<GetMailTips xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">");
getMailTipsSoapRequest.Append("<SendingAs>");
getMailTipsSoapRequest.Append("<t:EmailAddress>accessingemail#domain.com</t:EmailAddress>");
getMailTipsSoapRequest.Append("<t:RoutingType>SMTP</t:RoutingType></SendingAs>");
getMailTipsSoapRequest.Append("<Recipients><t:Mailbox>");
getMailTipsSoapRequest.Append("<t:EmailAddress>" + emailToCheck + "</t:EmailAddress>");
getMailTipsSoapRequest.Append("<t:RoutingType>SMTP</t:RoutingType></t:Mailbox></Recipients>");
getMailTipsSoapRequest.Append(" <MailTipsRequested>OutOfOfficeMessage</MailTipsRequested></GetMailTips>");
getMailTipsSoapRequest.Append("</soap:Body></soap:Envelope>");
streamWriter.Write(getMailTipsSoapRequest.ToString());
streamWriter.Close();
HttpWebResponse webResponse = (HttpWebResponse)httpwebRequest.GetResponse();
StreamReader streamreader = new StreamReader(webResponse.GetResponseStream());
string response = streamreader.ReadToEnd();
if (response.Contains("<t:Message/>"))
return null;
int messageIndex = response.IndexOf("<t:Message>");
response = response.Substring(messageIndex, response.IndexOf("</t:Message>") - messageIndex);
return response;
}
http://blogs.msdn.com/b/devmsg/archive/2014/06/03/ews-how-to-retrieve-the-oof-out-of-facility-settings-message-using-ews-for-an-exchange-user.aspx This one takes as a paramater, urlname and I'm not sure where that url is coming from. This one seems the most promising though, any ideas where that comes from?
urlname is just the EWS URL if you are using the Managed API than just use the value from service.url.
http://gsexdev.blogspot.com/2011/11/using-mailtips-in-ews-to-get-oof-out-of.html I don't have reference to ExchangeServiceBinding, even though I'm using Microsoft.Exchange.WebServices.Data;
This is proxy code generated from the Exchange Web Service WSDL file see https://msdn.microsoft.com/en-us/library/office/dd877040(v=exchg.140).aspx (Microsoft.Exchange.WebServices.Data) is the Managed API.
Solution worked great for me as supplied by Aaron. However returning the substring gave me issues since ours was an html string rather than plain text and it didn't parse correctly. So i replaced the StreamReader down portion with the following. This still returns as a string but correctly parses as the html it was returned as.
XDocument doc;
using (Stream responseStream = response.GetResponseStream())
{
doc = XDocument.Load(responseStream);
}
return doc.Root.Descendants().Where(d => d.Name.LocalName == "Message").Select(d => d.Value).FirstOrDefault();
I have created AccessToken using https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Example+-+OAuth+authentication tutorial and could connected and get the data from jar file. I need to access it with c# app. I tried following code but it wasn't working.
var request = (HttpWebRequest)WebRequest.Create(apiUrl);
request.Method = "GET";
request.Headers.Add("Authorization", "Bearer " + API_Access_Token);
var response = (HttpWebResponse)request.GetResponse();
string html = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
html = reader.ReadToEnd();
}
Original question was Access JIRA API with api key without username and password
Hi Im wondering if the azure blob service api http://msdn.microsoft.com/en-us/library/dd135733.aspx
can be called using c#. Id like to upload a file e.g a word document to a storage location, the http method is "put" and the rest url is
"http://myaccount.blob.core.windows.net/mycontainer/myblob"
would this code work?
string username = "user";
string password = "password";
string data = "path to word document;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "PUT";
request.Credentials = new NetworkCredential(username, password);
request.ContentLength = data.Length;
request.ContentType = "text/plain";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream( ))) {
writer.WriteLine(data);
}
WebResponse response = request.GetResponse( );
using (StreamReader reader = new StreamReader(response.GetResponseStream( ))) {
while (reader.Peek( ) != -1) {
Console.WriteLine(reader.ReadLine( ));
No, this wouldn't work. Authentication to Windows Azure storage involves signing the headers of the request. (See http://msdn.microsoft.com/en-us/library/dd179428.aspx.)
Note that the .NET StorageClient library that ships with the SDK is redistributable, so you can just add a reference to that and do (from memory):
CloudStorageAccount.Parse("<connection string>")
.CreateCloudBlobClient()
.GetBlobReference("mycontainer/myblob")
.UploadByteArray(data);