Ejemplo n.º 1
0
 /// <summary>
 /// Invoked whenever WeChat 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(QQAccountAuthenticatedContext context)
 {
     return(OnAuthenticated(context));
 }
Ejemplo n.º 2
0
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationProperties properties = null;

            try
            {
                string code  = null;
                string state = null;

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

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

                // OAuth2 10.12 CSRF
                if (!ValidateCorrelationId(Options.CookieManager, properties, _logger))
                {
                    return(new AuthenticationTicket(null, properties));
                }

                var tokenRequestParameters = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("client_id", Options.AppId),
                    new KeyValuePair <string, string>("redirect_uri", GenerateRedirectUri()),
                    new KeyValuePair <string, string>("client_secret", Options.AppSecret),
                    new KeyValuePair <string, string>("code", code),
                    new KeyValuePair <string, string>("grant_type", "authorization_code"),
                };

                var requestContent           = new FormUrlEncodedContent(tokenRequestParameters);
                HttpResponseMessage response = await _httpClient.PostAsync(Options.TokenEndpoint, requestContent, Request.CallCancelled);

                response.EnsureSuccessStatusCode();
                var oauthTokenResponse = await response.Content.ReadAsStringAsync();

                var tokenParams = oauthTokenResponse.Split('&');
                var oauth2Token = new Dictionary <string, string>();
                foreach (var tokenParam in tokenParams)
                {
                    oauth2Token.Add(tokenParam.Split('=')[0], tokenParam.Split('=')[1]);
                }

                var accessToken = oauth2Token["access_token"];
                // Refresh token is only available when wl.offline_access is request.
                // Otherwise, it is null.
                var refreshToken = oauth2Token["refresh_token"];
                var expire       = oauth2Token["expires_in"];

                if (string.IsNullOrWhiteSpace(accessToken))
                {
                    _logger.WriteWarning("Access token was not found");
                    return(new AuthenticationTicket(null, properties));
                }

                #region  Get OpenId
                //https://graph.qq.com/oauth2.0/me?access_token=
                var opneIdRequest  = new HttpRequestMessage(HttpMethod.Get, Options.OpenIdEndpoint + "?access_token=" + accessToken);
                var openIdResponse = await _httpClient.SendAsync(opneIdRequest, Request.CallCancelled);

                openIdResponse.EnsureSuccessStatusCode();
                string openIdString = await openIdResponse.Content.ReadAsStringAsync();

                //callback( {"client_id":"","openid":""} );

                Regex reg = new Regex("^callback\\( (.*) \\);$");
                //例如我想提取记录中的NAME值
                Match  match       = reg.Match(openIdString);
                string callbackObj = match.Groups[1].Value;

                JObject openIdInfo = JObject.Parse(callbackObj);
                var     openId     = openIdInfo["openid"].Value <string>();
                var     clientId   = openIdInfo["client_id"].Value <string>();

                if (string.IsNullOrWhiteSpace(openId))
                {
                    var description = openIdInfo["error_description"].Value <string>();
                    _logger.WriteWarning(description);
                    return(new AuthenticationTicket(null, properties));
                }
                #endregion

                #region Get UserInfo
                //https://graph.qq.com/user/get_user_info?access_token=415AC27295DF33B910826DC38C610798&oauth_consumer_key=101452737&openid=DCCB58B2D6FCDECF53C600702C7A5269

                var graphRequest  = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint + $"?access_token={accessToken}&oauth_consumer_key={clientId}&openid={openId}");
                var graphResponse = await _httpClient.SendAsync(graphRequest, Request.CallCancelled);

                graphResponse.EnsureSuccessStatusCode();
                string accountString = await graphResponse.Content.ReadAsStringAsync();

                JObject accountInformation = JObject.Parse(accountString);
                var     ret = accountInformation.Value <string>("ret");
                if (ret != "0")
                {
                    var msg = accountInformation.Value <string>("msg");
                    _logger.WriteWarning(msg);
                    return(new AuthenticationTicket(null, properties));
                }
                #endregion
                var context = new QQAccountAuthenticatedContext(Context, openId, accountInformation, accessToken,
                                                                refreshToken, expire);

                context.Identity = new ClaimsIdentity(
                    new[]
                {
                    //Private Key
                    new Claim(ClaimTypes.NameIdentifier, context.OpenId, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim(ClaimTypes.Name, context.NickName, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:openid", context.OpenId, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:nickname", context.NickName, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:gender", context.Gender, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:province", context.Province, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:city", context.City, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:year", context.Year, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:figureurl", context.FigureUrl, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:figureurl_1", context.FigureUrl_1, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:figureurl_2", context.FigureUrl_2, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:figureurl_qq_1", context.Figureurl_Qq_1, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:figureurl_qq_2", context.Figureurl_Qq_2, "http://www.w3.org/2001/XMLSchema#string", Options.AuthenticationType),
                    new Claim("qqaccount:is_yellow_vip", context.IsYellowVip, "http://www.w3.org/2001/XMLSchema#bool", Options.AuthenticationType),
                    new Claim("qqaccount:vip", context.Vip, "http://www.w3.org/2001/XMLSchema#bool", Options.AuthenticationType),
                    new Claim("qqaccount:yellow_vip_level", context.YellowVipLevel, "http://www.w3.org/2001/XMLSchema#int", Options.AuthenticationType),
                    new Claim("qqaccount:level", context.Level, "http://www.w3.org/2001/XMLSchema#int", Options.AuthenticationType),
                    new Claim("qqaccount:is_yellow_year_vip", context.IsYellowYearVip, "http://www.w3.org/2001/XMLSchema#bool", Options.AuthenticationType),
                }, Options.AuthenticationType, ClaimsIdentity.DefaultNameClaimType,
                    ClaimsIdentity.DefaultRoleClaimType);

                context.Properties = properties;
                await Options.Provider.Authenticated(context);

                return(new AuthenticationTicket(context.Identity, context.Properties));
            }
            catch (Exception ex)
            {
                _logger.WriteError("Authentication failed", ex);
                return(new AuthenticationTicket(null, properties));
            }
        }