Beispiel #1
0
 /// <summary>
 /// Invoked whenever Facebook succesfully authenticates a user
 /// </summary>
 /// <param name="context">Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.</param>
 /// <returns>A <see cref="Task"/> representing the completed operation.</returns>
 public virtual Task Authenticated(FitbitAuthenticatedContext context)
 {
     return(OnAuthenticated(context));
 }
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationProperties properties = null;

            try
            {
                string oauth_token    = null;
                string oauth_verifier = null;
                string state          = null;

                IReadableStringCollection query  = Request.Query;
                IList <string>            values = query.GetValues("oauth_token");
                if (values != null && values.Count == 1)
                {
                    oauth_token = values[0];
                }
                values = query.GetValues("oauth_verifier");
                if (values != null && values.Count == 1)
                {
                    oauth_verifier = values[0];
                }

                properties = Options.StateDataFormat.Unprotect(OAuthState);
                if (properties == null)
                {
                    return(null);
                }

                var requestPrefix = Request.Scheme + "://" + Request.Host;
                var redirectUri   = requestPrefix + Request.PathBase + Options.CallbackPath;

                var client = new OAuthRequest
                {
                    ConsumerKey     = Options.ClientId,
                    ConsumerSecret  = Options.ClientSecret,
                    Type            = OAuthRequestType.AccessToken,
                    SignatureMethod = OAuthSignatureMethod.HmacSha1,
                    RequestUrl      = AccessTokenEndpoint,
                    Version         = "1.0",
                    Method          = "POST",
                    Token           = oauth_token,
                    TokenSecret     = OAuthTokenSecret,
                    Verifier        = oauth_verifier
                };
                var auth = client.GetAuthorizationHeader().Replace("OAuth ", "");
                _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", auth);
                var response = _httpClient.PostAsync(client.RequestUrl, null).Result;
                response.EnsureSuccessStatusCode();

                var textArray         = response.Content.ReadAsStringAsync().Result.Split('&');
                var accessToken       = textArray.First(i => i.Contains("oauth_token=")).Replace("oauth_token=", "");
                var accessTokenSecret = textArray.First(i => i.Contains("oauth_token_secret=")).Replace("oauth_token_secret=", "");
                var encodedUserId     = textArray.First(i => i.Contains("encoded_user_id=")).Replace("encoded_user_id=", "");

                client = new OAuthRequest
                {
                    ConsumerKey     = Options.ClientId,
                    ConsumerSecret  = Options.ClientSecret,
                    Type            = OAuthRequestType.ProtectedResource,
                    SignatureMethod = OAuthSignatureMethod.HmacSha1,
                    RequestUrl      = SelfEndpointTemplate,
                    Version         = "1.0",
                    Method          = "GET",
                    Token           = accessToken,
                    TokenSecret     = accessTokenSecret
                };
                auth = client.GetAuthorizationHeader().Replace("OAuth ", "");
                _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", auth);
                response = _httpClient.GetAsync(client.RequestUrl).Result;
                response.EnsureSuccessStatusCode();

                var text = await response.Content.ReadAsStringAsync();

                dynamic self = JObject.Parse(text);
                JObject user = self.user;

                var context = new FitbitAuthenticatedContext(Context, user, accessToken, accessTokenSecret, encodedUserId)
                {
                    Identity = new ClaimsIdentity(
                        Options.AuthenticationType,
                        ClaimsIdentity.DefaultNameClaimType,
                        ClaimsIdentity.DefaultRoleClaimType)
                };

                if (!string.IsNullOrEmpty(context.Id))
                {
                    context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, context.Id, XmlSchemaString,
                                                        Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.FullName))
                {
                    context.Identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, context.FullName,
                                                        XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.FullName))
                {
                    context.Identity.AddClaim(new Claim("urn:fitbit:fullName", context.FullName, XmlSchemaString,
                                                        Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.Email))
                {
                    context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email, XmlSchemaString,
                                                        Options.AuthenticationType));
                }
                context.Properties = properties;

                await Options.Provider.Authenticated(context);

                return(new AuthenticationTicket(context.Identity, context.Properties));
            }
            catch (Exception ex)
            {
                _logger.WriteError(ex.Message);
            }
            return(new AuthenticationTicket(null, properties));
        }
 /// <summary>
 /// Invoked whenever Facebook succesfully authenticates a user
 /// </summary>
 /// <param name="context">Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.</param>
 /// <returns>A <see cref="Task"/> representing the completed operation.</returns>
 public virtual Task Authenticated(FitbitAuthenticatedContext context)
 {
     return OnAuthenticated(context);
 }