0

我正在开发一个使用 ASP.NET MVC(Restful Behavior)的 WPF 应用程序。MVC 应用程序正在使用基本身份验证。那么,如何验证我的 WPF 应用程序以访问 MVC Url?请建议。

谢谢

4

1 回答 1

1

您可以使用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);
    }
}
于 2013-04-07T09:16:11.253 回答