delete a Firebase user using C# - c#

Is it possible to delete a user account in Firebase in C#?
I can successfully create users but on the delete, I get INSUFFICIENT_PERMISSION bu I don't know how to provide the correct permissions.
The code for creating a user:
var httpWebRequest = (HttpWebRequest)WebRequest.Create(NEW_USER_URL);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var body = new
{
email = email,
password = password
};
string json = JObject.FromObject(body).ToString();
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
The code for deleting a user:
var httpWebRequest = (HttpWebRequest)WebRequest.Create(DELETE_USER_URL);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var body = new
{
localId = uUid
};
string json = JObject.FromObject(body).ToString();
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
The urls:
static string NEW_USER_URL = $#"https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key={ConfigurationManager.AppSettings["FireBaseProjectApiKey"]}";
static string DELETE_USER_URL = $#"https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key={ConfigurationManager.AppSettings["FireBaseProjectApiKey"]}";

Related

How to pass data.bind in POST request using C#

i have a web app connected with crm Dynamics 365. I want to create record in an entity using API, but the entity has lookup fields to another entities. I tried this code but it gives bad request error. How can i send the values of the lookup fields in the API?
JSON:{
"msdyn_name": "Asset",
"msdyn_account#odata.bind": "/accounts(7f7e031b-0e20-ea11-a810-000d3a2d54fd)",
"new_externalproj#odata.bind": "/msdynce_externalprojects(ee3f03c6-751f-ea11-a810-000d3a27b751)" }
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(new
{
msdyn_name = "Asset",
msdyn_account = "7f7e031b-0e20-ea11-a810-000d3a2d54fd",
new_externalproj = "ee3f03c6-751f-ea11-a810-000d3a27b751"
});
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://XXX.dynamics.com/api/data/v9.1/msdyn_customerassets");
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization", String.Format("Bearer {0}", result.AccessToken));
httpWebRequest.Headers.Add("OData-MaxVersion", "4.0");
httpWebRequest.Headers.Add("OData-Version", "4.0");
httpWebRequest.ContentType = "application/json";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
}

Web API Authorization via HttpWebRequest

I have a function to call my Web API. It works well if TestCallingRemotely is set to [AllowAnonymous].
var httpWebRequest = (HttpWebRequest)WebRequest.Create(
"http://localhost/api/services/myApp/commonLookup/TestCallingRemotely");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
string input = "{}";
streamWriter.Write(input);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
How do I pass the username and password to the HttpWebRequest for authorization?
I need to call my Web API from CLR integration, which only supports System.Net.
ABP's startup template uses bearer token authentication infrastructure.
var token = GetToken(username, password);
// var httpWebRequest = (HttpWebRequest)WebRequest.Create(
// "http://localhost/api/services/myApp/commonLookup/TestCallingRemotely");
// httpWebRequest.ContentType = "application/json";
// httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization", "Bearer " + token);
// ...
Get token
This uses a crude way to extract the token, inspired by an MSDN article.
private string GetToken(string username, string password, string tenancyName = null)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(
"http://localhost:6334/api/Account/Authenticate");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var input = "{\"usernameOrEmailAddress\":\"" + username + "\"," +
"\"password\":\"" + password + "\"}";
if (tenancyName != null)
{
input = input.TrimEnd('}') + "," +
"\"tenancyName\":\"" + tenancyName + "\"}";
}
streamWriter.Write(input);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string response;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
response = streamReader.ReadToEnd();
}
// Crude way
var entries = response.TrimStart('{').TrimEnd('}').Replace("\"", String.Empty).Split(',');
foreach (var entry in entries)
{
if (entry.Split(':')[0] == "result")
{
return entry.Split(':')[1];
}
}
return null;
}
If the server uses basic authentication you can add the header like this:
var httpWebRequest = (HttpWebRequest) WebRequest.Create(
"http://localhost/api/services/myApp/commonLookup/TestCallingRemotely");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
var username = "Aladdin";
var password = "opensesame";
var bytes = Encoding.UTF8.GetBytes($"{username}:{password}");
httpWebRequest.Headers.Add("Authorization", $"Basic {Convert.ToBase64String(bytes)}");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string input = "{}";
streamWriter.Write(input);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

How to make a Log of a Post in an web Api

I would like to know how I can know what the Web Api is receiving in the Post that I am sending.
This is the code I'm using:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://d6dc30b8-0ee0-4-231-b9ee.azurewebsites.net/");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "application/vnd.lyoness.servicesv1 + json";
httpWebRequest.Headers.Add("Date" + tempo);
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
MessageBox.Show(json);
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var teste = streamReader.ReadToEnd();
MessageBox.Show(teste);
}
Keeping your current model, if you're just after a simple record you could do something like this
string url = "https://d6dc30b8-0ee0-4-231-b9ee.azurewebsites.net/";
string method = "POST";
string contentType = "application/json";
string accept = "application/vnd.lyoness.servicesv1 + json";
string headers = "Date";
string myLog = string.Format("URL = {0} | METHOD = {1} | CONTENT TYPE = {2} | ACCEPT = {3} | HEADERS = {4} + {5}", url, method, contentType, accept, headers, tempo);
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = method;
httpWebRequest.ContentType = contentType;
httpWebRequest.Accept = accept;
httpWebRequest.Headers.Add(headers + tempo);
Then you could write myLog to a text file or something to keep a record of it.

How do I send a json array to an ASP.NET server?

I wrote this code to send some JSON to a server:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.brilliantit.ir/getDetail.aspx");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
//string json = "{\"id\":\"ahbarres\"}";
string json = "{\"id\":\""+label1.Text+"\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
String temp = "";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
temp = result.ToString();
}
But, I want to send a JSON array to a server. How can I do this?

HttpWebRequest with proxy - setting cookies

Here is my code:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/jsonrpc.cgi");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "someParameters";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
}
string Bugzilla_logincookie= httpResponse.Headers.ToString();
Bugzilla_logincookie= Bugzilla_logincookie.Substring(plsWork .IndexOf("logincookie") + 12);
Bugzilla_logincookie= Bugzilla_logincookie.Substring(0, plsWork .IndexOf(";"));
CookieContainer cc = new CookieContainer();
cc.SetCookies(new Uri("http://localhost"), Bugzilla_logincookie);
var httpWebRequest2 = (HttpWebRequest)WebRequest.Create("http://localhost/jsonrpc.cgi");
httpWebRequest2.ContentType = "application/json";
httpWebRequest2.Method = "POST";
httpWebRequest2.Proxy.Credentials = new NetworkCredential("username", "password");
httpWebRequest2.CookieContainer = cc;
using (var streamWriter2 = new StreamWriter(httpWebRequest2.GetRequestStream()))
{
string json = "someParametersForJsonCall";
streamWriter2.Write(json);
}
var httpResponse2 = (HttpWebResponse)httpWebRequest2.GetResponse();
using (var streamReader2 = new StreamReader(httpResponse2.GetResponseStream()))
{
var responseText = streamReader2.ReadToEnd();
}
I have problem with using proxy. The thing I'm trying to do is: use a Proxy for http://www.bugzilla.org/docs/tip/en/html/api/Bugzilla/WebService/User.html to call the login method and then store cookies of response and send them with each call of the session.
I get this error:
"You must log in before using this part of Bugzilla."
What am I mistakenly using?

Categories

Resources