我想实现以下功能:从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操作无法识别用户。