public string GetResponseHtml(IDictionary<string, string> parameters, Uri signinUri) { string code = parameters["code"]; // Exchange the Request Token for an Access Token string appId = _settings.VkApplicationId; string appSecret = _settings.VkApplicationSecret; string scheme = parameters["SERVER_PORT_SECURE"] == "1" ? "https" : "http"; var callbackUri = new UriBuilder(string.Format("{0}://{1}", scheme, parameters["HTTP_HOST"])) { Path = parameters["URL"], Query = string.Format("context={0}", parameters["context"]) }; var service = new VkClient(appId, appSecret); dynamic accessToken = service.GetAccessToken(code, callbackUri.ToString()); dynamic token = accessToken.access_token; service.AuthenticateWith(token.ToString()); // Claims dynamic result = service.Get("users.get", new { fields = "screen_name" }); dynamic user = result.response[0]; string acsNamespace = _settings.AcsNamespace; string wtRealm = string.Format(WtRealm, acsNamespace); string wReply = string.Format(WReply, acsNamespace); var requestMessage = new SignInRequestMessage(signinUri, wtRealm, wReply); // Add extracted claims var identity = new ClaimsIdentity(AuthenticationTypes.Federation); identity.AddClaim(new Claim(ClaimTypes.Name, string.Format("{0} {1}", user.first_name, user.last_name))); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.uid.ToString())); identity.AddClaim(new Claim(VkClaims.VkToken, token.ToString())); var principal = new ClaimsPrincipal(identity); SignInResponseMessage responseMessage = FederatedPassiveSecurityTokenServiceOperations.ProcessSignInRequest(requestMessage, principal, this); responseMessage.Context = parameters["context"]; return responseMessage.WriteFormPost(); }
public TokenData Get(IpData data) { // Try to validate token var service = new VkClient(); service.AuthenticateWith(data.Token); dynamic result; try { result = service.Get("users.get", new { fields = "screen_name" }); } catch (Exception e) { throw new BadGatewayException(e); } if (result.error != null) { dynamic message = result.error.error_msg ?? string.Empty; throw new BadRequestException(message.ToString()); } try { dynamic user = result.response[0]; return new TokenData { IdentityProvider = ProviderType.Vk, Name = string.Format("{0} {1}", user.first_name, user.last_name), UserIdentifier = user.uid.ToString(), Token = data.Token, TokenSecret = data.TokenSecret }; } catch (Exception e) { string message = string.Format("Unable to receive VK profile: {0}", e); throw new InternalServerErrorException(message); } }