【发布时间】:2015-02-03 20:25:13
【问题描述】:
有很多关于通过 Windows 应用程序从 Web.Api 获取数据的示例,但我所看到的都是使用不需要用户身份验证的控件。
在我的情况下,我需要从 Windows 应用程序获取数据,但我需要调用的 Web.Api 方法是需要用户认证的控制器方法(控制器由[Authorize] 属性修饰)。
我在这里找到了一个关于获取数据的简单示例:http://www.asp.net/web-api/overview/mobile-clients/calling-web-api-from-a-windows-phone-8-application
但那里没有关于用户的信息:
public void LoadData()
{
if (this.IsDataLoaded == false)
{
this.Items.Clear();
this.Items.Add(new ItemViewModel() { ID = "0", LineOne = "Please Wait...", LineTwo = "Please wait while the catalog is downloaded from the server.", LineThree = null });
WebClient webClient = new WebClient();
webClient.Headers["Accept"] = "application/json";
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadCatalogCompleted);
webClient.DownloadStringAsync(new Uri(apiUrl));
}
}
private void webClient_DownloadCatalogCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
this.Items.Clear();
if (e.Result != null)
{
var books = JsonConvert.DeserializeObject<BookDetails[]>(e.Result);
int id = 0;
foreach (BookDetails book in books)
{
this.Items.Add(new ItemViewModel()
{
ID = (id++).ToString(),
LineOne = book.Title,
LineTwo = book.Author,
LineThree = book.Description.Replace("\n", " ")
});
}
this.IsDataLoaded = true;
}
}
catch (Exception ex)
{
this.Items.Add(new ItemViewModel()
{
ID = "0",
LineOne = "An Error Occurred",
LineTwo = String.Format("The following exception occured: {0}", ex.Message),
LineThree = String.Format("Additional inner exception information: {0}", ex.InnerException.Message)
});
}
}
如何完成LoadData方法,以便通过谷歌认证后得到结果? (微软?)
【问题讨论】: