Can’t call any method of authenticate webapi, even login or register

By Prabhath Withanage

i have develop and created web api with authentication, but when i try to register to it using console application i get following error

response = {StatusCode: 401, ReasonPhrase: ‘Unauthorized’, Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
X-SourceFiles: =?UTF-8?B?RDpcU1RFQ0hfUHJvamVjdFxQcm9qZWN0c1xMb3lhbHR5XFNhbnN1dGVjaC5MdHkuRGF0YS5XZWIuQXBpXGFwaVxBY2NvdW50XFJlZ2lzdGVy?=
Cache-Control: private
Server: Microsoft-IIS/8.0
WWW-Authenticate: Bearer, Negotiate, NTLM
X-Powered-By: ASP.NET

how can i register to api using console client

i can call register method from postman and get register to the application

here is the my console code to register

static async Task<string> PostRequest4(string token, string apiBaseUri, string requestPath, RegisterBindingModel t)
    {
        HttpClientHandler hndlr = new HttpClientHandler();
        hndlr.UseDefaultCredentials = true;
        using (var client = new HttpClient())
        {
            //setup client
            client.BaseAddress = new Uri("http://localhost:38811/");
            client.DefaultRequestHeaders.Accept.Clear();
            //client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //make request
            HttpResponseMessage response = await client.PostAsJsonAsync(requestPath, t);
            //make request
            Stream stream = await response.Content.ReadAsStreamAsync();
            DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(ResponseMessage));
            ResponseMessage objResponse = (ResponseMessage)dataContractJsonSerializer.ReadObject(stream);
            string res = "";
            if (objResponse == null)
            {
                res = "error occurred!";
            }
            else
            {
                if (objResponse.ErrorMessage != null)
                {
                    res = "error occurred!";
                }
                else if (objResponse.IsSuccess)
                {
                    res = "Item ID " + objResponse.ID.ToString() + " have been added Successfully!";
                }
            }
            return res;
        }
    }

before pass if i add following line then i can successfully register to the application

client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

Here i got token by the following method using an existing user “user name and password”

private static async Task<string> GetAPIToken(string userName, string password, string apiBaseUri)
    {
        using (var client = new HttpClient())
        {
            //setup client
            client.BaseAddress = new Uri(apiBaseUri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //setup login data
            var formContent = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("grant_type", "password"),
                new KeyValuePair<string, string>("username", userName),
                new KeyValuePair<string, string>("password", password),
            });
            //send request
            HttpResponseMessage responseMessage = await client.PostAsync(Token , formContent);
            //get access token from response body
            var responseJson = await responseMessage.Content.ReadAsStringAsync();
            var jObject = JObject.Parse(responseJson);
            return jObject.GetValue("access_token").ToString();
        }
    }

i want to call register method without involving existing user(user name and password)

I will be very grateful if somebody can help me.
~Thanks~

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273