net iactionresult created code asp applicationuser asp.net api windows-phone-8.1 liveid

asp.net - iactionresult - Ingresar desde Universal App a Web Api usando Live Id



return created asp net core (1)

Estoy tratando de implementar la siguiente funcionalidad:

  1. El usuario inicia sesión en la cuenta de Live Id desde la aplicación Windows Phone 8.1 (o Universal).
  2. La aplicación accede a la API web que desarrollo con ASP.NET Web Api 2
  3. En este API web necesito autenticar al usuario.
  4. Más tarde, quiero autenticar al mismo usuario en la aplicación web

Esto es lo que estoy haciendo, y no funciona.

En mi aplicación para teléfono de Windows:

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

Y entonces

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

Y luego en mi API web tengo siguiente

public void ConfigureAuth(IAppBuilder app) { app.UseMicrosoftAccountAuthentication( clientId: "my client id", clientSecret: "my secret"); }

Registré http://myservice.cloudapp.net como redirigir url.

El problema es que la autenticación no funciona, web api actions dp no reconoce al usuario.

Agradecería que alguien me señale lo que estoy haciendo mal


Lo entendí del todo mal. Primero, realmente necesito usar el método app.UseJwtBearerAuthentication. El ejemplo se encontró aquí http://code.lawrab.com/2014/01/securing-webapi-with-live-id.html . Pero cuando lo intenté, obtuve este error en la salida

IDX10500: Signature validation failed. Unable to resolve SecurityKeyIdentifier: ''SecurityKeyIdentifier ( IsReadOnly = False, Count = 1, Clause[0] = System.IdentityModel.Tokens.NamedKeySecurityKeyIdentifierClause )

Esto me llevó un tiempo averiguarlo, hasta que encontré esta publicación: JwtSecurityTokenHandler 4.0.0 ¿Rompiendo cambios?

Al unir estas cosas, obtuve la solución que parece funcionar ahora en mi entorno de prueba:

public void ConfigureAuth(IAppBuilder app) { var sha256 = new SHA256Managed(); var sKey = "<Secret key>" + "JWTSig"; var secretBytes = new UTF8Encoding(true, true).GetBytes(sKey); var signingKey = sha256.ComputeHash(secretBytes); var securityKeyProvider = new SymmetricKeyIssuerSecurityTokenProvider("urn:windows:liveid", signingKey); var securityKey = securityKeyProvider.SecurityTokens.First().SecurityKeys.First(); var jwtOptions = new JwtBearerAuthenticationOptions() { //AllowedAudiences = new[] { "<url>" }, //IssuerSecurityTokenProviders = new[] //{ // new SymmetricKeyIssuerSecurityTokenProvider("urn:windows:liveid",signingKey) //}, TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters() { IssuerSigningKeyResolver = (token, securityToken, keyIdentifier, validationParameters) => { return securityKey; }, ValidAudience = "<url>", ValidIssuer = securityKeyProvider.Issuer } }; app.UseJwtBearerAuthentication(jwtOptions); }