我想實現以下功能:從Windows Phone的8.1(或通用)應用在登錄網頁API使用Live標識
- 用戶登錄到Live ID帳戶。
- 應用程序訪問我使用ASP.NET Web Api開發的Web Api 2
- 在此Web Api中,我需要驗證用戶。
- 後來,我想驗證的Web應用程序相同的用戶
下面是我在做什麼,而這是行不通的。
在我的Windows Phone應用程序:
var authClient = new LiveAuthClient("http://myservice.cloudapp.net");
LiveLoginResult result = await authClient.LoginAsync(new string[] { "wl.signin" });
if (result.Status == LiveConnectSessionStatus.Connected)
{
connected = true;
var identity = await ConnectToApi(result.Session.AuthenticationToken);
Debug.WriteLine(identity);
}
然後
private async Task<string> ConnectToApi(string token)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://myservice.cloudapp.net/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
// HTTP GET
HttpResponseMessage response = await client.GetAsync("api/values");
if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
return result;
}
else
return response.ReasonPhrase;
}
}
然後在我的網頁API我有以下
public void ConfigureAuth(IAppBuilder app)
{
app.UseMicrosoftAccountAuthentication(
clientId: "my client id",
clientSecret: "my secret");
}
我註冊http://myservice.cloudapp.net爲重定向URL。
問題是身份驗證不起作用,web api操作無法識別用戶。