newonehere69
New member
- Joined
- Nov 28, 2016
- Messages
- 2
- Programming Experience
- Beginner
Hi,
I am new to C# and I need to know how I can pass a user information to a async method.
Any ideas how I can accomplish this?
Thanks!
I am new to C# and I need to know how I can pass a user information to a async method.
C#:
//User Class
public class User
{
[JsonProperty("username")]
public string UserName { get; set; }
[JsonProperty("firstname")]
public string FirstName { get; set; }
[JsonProperty("lastname")]
public string LastName { get; set; }
public async static Task<bool> Create(User user)
{
var byteArray = Encoding.ASCII.GetBytes($"{Username}:{Password}");
string load = JsonConvert.SerializeObject(user);
StringContent c = new StringContent(load, Encoding.UTF8, "application/json");
string temp = String.Empty;
using (HttpClient http = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
http.DefaultRequestHeaders.Add("Api-Key", ApiKey);
HttpResponseMessage res = http.PutAsync(RESTUrl + "Users", c).Result;
if (res.IsSuccessStatusCode)
{
}
if (res.Content != null)
{
string resContent = res.Content.ReadAsStringAsync().Result;
temp = resContent;
}
return res.IsSuccessStatusCode;
}
}
}
//On Default Page Code Behind PageLoad
string uname = HttpContext.Current.User.Identity.Name;
//Info is a separate class that gets the user information
Info info = new Info(uname);
//I need to pass that user information (username,firstname, lastname, ect..) to the async method that is on the users class(top)
//This is what I've been trying and is where the confusion is.
/The Create method takes a parameter of type User. It must pass a parameter of type User.
bool x = await User.Create(info);
Any ideas how I can accomplish this?
Thanks!