I'm trying to implement following functionality:

  1. User signs in into Live Id account from Windows Phone 8.1 (or Universal) app.
  2. App accesses Web Api that I develop with ASP.NET Web Api 2
  3. In this Web Api I need to authenticate the user.
  4. Later, I want to authenticate same user in web app

Here is what I'm doing, and it doesn't work.

In my Windows Phone App:

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);
                }

And then

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;
    }
}

And then in my web api I have following

public void ConfigureAuth(IAppBuilder app)
{

    app.UseMicrosoftAccountAuthentication(
        clientId: "my client id",
        clientSecret: "my secret");

}

I registered http://myservice.cloudapp.net as redirect url.

The problem is authentication doesn't work, web api actions dp not recognize the user.

I would appreciate if someone could point me into what I'm doing wrong

Related posts

Recent Viewed