我正在尝试实现以下功能:
- 用户从 Windows Phone 8.1(或通用)应用程序登录 Live Id 帐户。
- 应用程序访问我使用 ASP.NET Web Api 2 开发的 Web Api
- 在这个 Web Api 中,我需要对用户进行身份验证。
- 稍后,我想在网络应用程序中验证同一用户
这是我正在做的,它不起作用。
在我的 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 操作无法识别用户。