0

I'm working on a WPF application which is consuming an ASP.NET MVC (Restful Behavior). MVC application is using Basic Authentication. So, how can I authenticate my WPF application to access MVC Url? Please suggest.

Thanks

1 Answer 1

1

You could use an HttpClient:

using (var client = new HttpClient())
{
    var username = "john";
    var password = "secret";

    var buffer = Encoding.ASCII.GetBytes(string.Concat(username, ":", password));
    var authHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(buffer));
    client.DefaultRequestHeaders.Authorization = authHeader;
    var task = client.GetAsync("https://example.com/somemethod");
    if (task.Result.StatusCode == HttpStatusCode.Unauthorized)
    {
        Console.WriteLine("wrong credentials");
    }
    else
    {
        task.Result.EnsureSuccessStatusCode();
        Console.WriteLine(task.Result.Content.ReadAsAsync<string>().Result);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.