コード例 #1
0
 /// <summary>
 /// Initializes a <see cref="T:Taitans.Owin.Security.QQ.QQOAuth2AuthenticatedContext" />
 /// </summary>
 /// <param name="context">The OWIN environment</param>
 /// <param name="openId">QQ openid</param>
 /// <param name="user">The JSON-serialized QQ user info</param>
 /// <param name="tokenResponse">The JSON-serialized token response QQ</param>
 public QQOAuth2AuthenticatedContext(IOwinContext context, string openId, JObject user, JObject tokenResponse) : base(context)
 {
     this.User          = user;
     this.TokenResponse = tokenResponse;
     if (tokenResponse != null)
     {
         this.AccessToken  = tokenResponse.Value <string>("access_token");
         this.RefreshToken = tokenResponse.Value <string>("refresh_token");
         int num;
         if (int.TryParse(tokenResponse.Value <string>("expires_in"), NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
         {
             this.ExpiresIn = new TimeSpan?(TimeSpan.FromSeconds((double)num));
         }
     }
     this.Id              = openId;
     this.Name            = QQOAuth2AuthenticatedContext.TryGetValue(user, "nickname");
     this.Province        = QQOAuth2AuthenticatedContext.TryGetValue(user, "province");
     this.City            = QQOAuth2AuthenticatedContext.TryGetValue(user, "city");
     this.Gender          = QQOAuth2AuthenticatedContext.TryGetValue(user, "gender");
     this.Year            = QQOAuth2AuthenticatedContext.TryGetValue(user, "year");
     this.Figureurl       = QQOAuth2AuthenticatedContext.TryGetValue(user, "figureurl");
     this.Figureurl_1     = QQOAuth2AuthenticatedContext.TryGetValue(user, "figureurl_1");
     this.Figureurl_2     = QQOAuth2AuthenticatedContext.TryGetValue(user, "figureurl_2");
     this.Figureurl_QQ_1  = QQOAuth2AuthenticatedContext.TryGetValue(user, "figureurl_qq_1");
     this.Figureurl_QQ_2  = QQOAuth2AuthenticatedContext.TryGetValue(user, "figureurl_qq_2");
     this.IsYellowVip     = QQOAuth2AuthenticatedContext.TryGetValue(user, "is_yellow_vip");
     this.Vip             = QQOAuth2AuthenticatedContext.TryGetValue(user, "vip");
     this.YellowVipLevel  = QQOAuth2AuthenticatedContext.TryGetValue(user, "yellow_vip_level");
     this.Level           = QQOAuth2AuthenticatedContext.TryGetValue(user, "level");
     this.IsYellowYearVip = QQOAuth2AuthenticatedContext.TryGetValue(user, "is_yellow_year_vip");
     this.Email           = Name;
 }
コード例 #2
0
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationProperties authenticationProperties = null;
            AuthenticationTicket     result;

            try
            {
                string value                     = null;
                string protectedText             = null;
                IReadableStringCollection query  = base.Request.Query;
                IList <string>            values = query.GetValues("code");
                if (values != null && values.Count == 1)
                {
                    value = values[0];
                }
                values = query.GetValues("state");
                if (values != null && values.Count == 1)
                {
                    protectedText = values[0];
                }
                authenticationProperties = base.Options.StateDataFormat.Unprotect(protectedText);
                if (authenticationProperties == null)
                {
                    result = null;
                }
                else if (!base.ValidateCorrelationId(authenticationProperties, this._logger))
                {
                    result = new AuthenticationTicket(null, authenticationProperties);
                }
                else
                {
                    string arg    = base.Request.Scheme + "://" + base.Request.Host;
                    string value2 = arg + base.RequestPathBase + base.Options.CallbackPath;
                    List <KeyValuePair <string, string> > list = new List <KeyValuePair <string, string> >();
                    list.Add(new KeyValuePair <string, string>("grant_type", "authorization_code"));
                    list.Add(new KeyValuePair <string, string>("code", value));
                    list.Add(new KeyValuePair <string, string>("redirect_uri", value2));
                    list.Add(new KeyValuePair <string, string>("client_id", base.Options.ClientId));
                    list.Add(new KeyValuePair <string, string>("client_secret", base.Options.ClientSecret));
                    HttpResponseMessage httpResponseMessage = await this._httpClient.PostAsync(TokenEndpoint, new FormUrlEncodedContent(list));

                    httpResponseMessage.EnsureSuccessStatusCode();
                    string text = await TokenToJsonText(await httpResponseMessage.Content.ReadAsStringAsync());

                    JObject jObject = JObject.Parse(text);
                    string  text2   = jObject.Value <string>("access_token");
                    if (string.IsNullOrWhiteSpace(text2))
                    {
                        LoggerExtensions.WriteWarning(this._logger, "Access token was not found", new string[0]);
                        result = new AuthenticationTicket(null, authenticationProperties);
                    }
                    else
                    {
                        HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, $"{OpenIdEndpoint}?access_token={text2}");
                        //httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", text2);
                        HttpResponseMessage httpResponseMessage2 = await this._httpClient.SendAsync(httpRequestMessage, base.Request.CallCancelled);

                        httpResponseMessage2.EnsureSuccessStatusCode();
                        text = await OpenIdToJsonText(await httpResponseMessage2.Content.ReadAsStringAsync());

                        jObject = JObject.Parse(text);
                        string openid = jObject.Value <string>("openid");
                        if (string.IsNullOrWhiteSpace(openid))
                        {
                            LoggerExtensions.WriteWarning(this._logger, "Access token was not found", new string[0]);
                            result = new AuthenticationTicket(null, authenticationProperties);
                        }
                        else
                        {
                            httpRequestMessage   = new HttpRequestMessage(HttpMethod.Get, $"{UserInfoEndpoint}?access_token={text2}&oauth_consumer_key={base.Options.ClientId}&openid={openid}");
                            httpResponseMessage2 = await this._httpClient.SendAsync(httpRequestMessage, base.Request.CallCancelled);

                            text = await httpResponseMessage2.Content.ReadAsStringAsync();

                            JObject user = JObject.Parse(text);
                            QQOAuth2AuthenticatedContext qqOAuth2AuthenticatedContext = new QQOAuth2AuthenticatedContext(base.Context, openid, user, jObject);
                            qqOAuth2AuthenticatedContext.Identity = new ClaimsIdentity(base.Options.AuthenticationType, "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "http://schemas.microsoft.com/ws/2008/06/identity/claims/role");
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Id))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", qqOAuth2AuthenticatedContext.Id, "http://www.w3.org/2001/XMLSchema#string", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Name))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", qqOAuth2AuthenticatedContext.Name, "http://www.w3.org/2001/XMLSchema#string", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Email))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", qqOAuth2AuthenticatedContext.Email, "http://www.w3.org/2001/XMLSchema#string", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Gender))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:gender", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#gender", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Province))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:province", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#province", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.City))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:city", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#city", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Year))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:year", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#year", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Figureurl))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:figureurl", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#figureurl", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Figureurl_1))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:figureurl_1", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#figureurl_1", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Figureurl_2))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:figureurl_2", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#figureurl_2", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Figureurl_QQ_1))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:figureurl_qq_1", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#figureurl_qq_1", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Figureurl_QQ_2))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:figureurl_qq_2", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#figureurl_qq_2", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.IsYellowVip))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:is_yellow_vip", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#is_yellow_vip", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Vip))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:vip", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#vip", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.YellowVipLevel))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:yellow_vip_level", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#yellow_vip_level", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.Level))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:level", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#level", base.Options.AuthenticationType));
                            }
                            if (!string.IsNullOrEmpty(qqOAuth2AuthenticatedContext.IsYellowYearVip))
                            {
                                qqOAuth2AuthenticatedContext.Identity.AddClaim(new Claim("urn:qq:is_yellow_year_vip", qqOAuth2AuthenticatedContext.City, "http://www.w3.org/2001/XMLSchema#is_yellow_year_vip", base.Options.AuthenticationType));
                            }
                            qqOAuth2AuthenticatedContext.Properties = authenticationProperties;
                            await base.Options.Provider.Authenticated(qqOAuth2AuthenticatedContext);

                            result = new AuthenticationTicket(qqOAuth2AuthenticatedContext.Identity, qqOAuth2AuthenticatedContext.Properties);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerExtensions.WriteError(this._logger, "Authentication failed", ex);
                result = new AuthenticationTicket(null, authenticationProperties);
            }
            return(result);
        }
コード例 #3
0
 /// <summary>
 /// Invoked whenever QQ succesfully authenticates a user
 /// </summary>
 /// <param name="context">Contains information about the login session as well as the user <see cref="T:System.Security.Claims.ClaimsIdentity" />.</param>
 /// <returns>A <see cref="T:System.Threading.Tasks.Task" /> representing the completed operation.</returns>
 public virtual Task Authenticated(QQOAuth2AuthenticatedContext context)
 {
     return(this.OnAuthenticated(context));
 }