コード例 #1
0
        protected virtual async Task <AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, AccessToken token)
        {
            var notification = new TwitterAuthenticatedContext(Context, token.UserId, token.ScreenName, token.Token, token.TokenSecret)
            {
                Principal  = new ClaimsPrincipal(identity),
                Properties = properties
            };

            await Options.Notifications.Authenticated(notification);

            if (notification.Principal?.Identity == null)
            {
                return(null);
            }

            return(new AuthenticationTicket(notification.Principal, notification.Properties, Options.AuthenticationScheme));
        }
コード例 #2
0
 /// <summary>
 /// Invoked whenever Twitter 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(TwitterAuthenticatedContext context)
 {
     return(OnAuthenticated(context));
 }
コード例 #3
0
 /// <summary>
 /// Invoked whenever Twitter 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(TwitterAuthenticatedContext context)
 {
     return OnAuthenticated(context);
 }
コード例 #4
0
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationProperties properties = null;

            try
            {
                IReadableStringCollection query = Request.Query;
                string protectedRequestToken    = Request.Cookies[StateCookie];

                RequestToken requestToken = Options.StateDataFormat.Unprotect(protectedRequestToken);

                if (requestToken == null)
                {
                    _logger.LogWarning("Invalid state");
                    return(null);
                }

                properties = requestToken.Properties;

                string returnedToken = query.Get("oauth_token");
                if (string.IsNullOrWhiteSpace(returnedToken))
                {
                    _logger.LogWarning("Missing oauth_token");
                    return(new AuthenticationTicket(properties, Options.AuthenticationScheme));
                }

                if (returnedToken != requestToken.Token)
                {
                    _logger.LogWarning("Unmatched token");
                    return(new AuthenticationTicket(properties, Options.AuthenticationScheme));
                }

                string oauthVerifier = query.Get("oauth_verifier");
                if (string.IsNullOrWhiteSpace(oauthVerifier))
                {
                    _logger.LogWarning("Missing or blank oauth_verifier");
                    return(new AuthenticationTicket(properties, Options.AuthenticationScheme));
                }

                AccessToken accessToken = await ObtainAccessTokenAsync(Options.ConsumerKey, Options.ConsumerSecret, requestToken, oauthVerifier);

                var context = new TwitterAuthenticatedContext(Context, accessToken.UserId, accessToken.ScreenName, accessToken.Token, accessToken.TokenSecret);

                context.Principal = new ClaimsPrincipal(
                    new ClaimsIdentity(
                        new[]
                {
                    new Claim(ClaimTypes.NameIdentifier, accessToken.UserId, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationScheme),
                    new Claim(ClaimTypes.Name, accessToken.ScreenName, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationScheme),
                    new Claim("urn:twitter:userid", accessToken.UserId, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationScheme),
                    new Claim("urn:twitter:screenname", accessToken.ScreenName, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationScheme)
                },
                        Options.AuthenticationScheme,
                        ClaimsIdentity.DefaultNameClaimType,
                        ClaimsIdentity.DefaultRoleClaimType));
                context.Properties = requestToken.Properties;

                var cookieOptions = new CookieOptions
                {
                    HttpOnly = true,
                    Secure   = Request.IsHttps
                };

                Response.Cookies.Delete(StateCookie, cookieOptions);

                await Options.Notifications.Authenticated(context);

                return(new AuthenticationTicket(context.Principal, context.Properties, Options.AuthenticationScheme));
            }
            catch (Exception ex)
            {
                _logger.LogError("Authentication failed", ex);
                return(new AuthenticationTicket(properties, Options.AuthenticationScheme));
            }
        }