locked
Authentication from Windows Phone App in Web Api. RRS feed

  • Question

  • I'm trying to use Live authentication on my Windows Phone 8.1 app, and then use this authentication to my Web Api.

    This is my Windows Phone code:

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

    Which uses this method:

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

    In my Api I use this simple code:

    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

    Wednesday, March 4, 2015 5:03 AM

Answers

All replies