예제 #1
0
 public Task Authenticated(SinaAuthenticatedContext context)
 {
     return this.OnAuthenticated(context);
 }
        protected async override Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationProperties properties = null;
            AuthenticationTicket     authenticationTicket;

            IReadableStringCollection query = this.Request.Query;

            properties = this.UnpackStateParameter(query);
            string         code   = string.Empty;
            IList <string> values = query.GetValues("code");

            if (values != null && values.Count == 1)
            {
                code = values[0];
            }
            if (string.IsNullOrEmpty(code))
            {
                authenticationTicket = new AuthenticationTicket(null, properties);
                return(authenticationTicket);
            }

            if (properties == null)
            {
                authenticationTicket = null;
            }
            else if (!this.ValidateCorrelationId(properties, this._logger))
            {
                authenticationTicket = new AuthenticationTicket(null, properties);
            }
            else
            {
                string tokenEndpoint = "https://api.weibo.com/oauth2/access_token?client_id={0}&client_secret={1}&grant_type=authorization_code&redirect_uri={3}&code={2}";
                var    url           = string.Format(
                    tokenEndpoint,
                    Uri.EscapeDataString(this.Options.AppID),
                    Uri.EscapeDataString(this.Options.AppKey),
                    Uri.EscapeDataString(code), Uri.EscapeDataString("http://" + this.Request.Host));
                HttpResponseMessage tokenResponse = await this._httpClient.PostAsync(url, new StringContent(""), this.Request.CallCancelled);

                tokenResponse.EnsureSuccessStatusCode();
                string access_tokenReturnValue = await tokenResponse.Content.ReadAsStringAsync();

                var accesstokenpa = "\"access_token\":\"(.+?)\"";
                var accesstoken   = Regex.Match(access_tokenReturnValue, accesstokenpa).Groups[1].Value;
                var uidpa         = "\"uid\":\"(.+?)\"";
                var openid        = Regex.Match(access_tokenReturnValue, uidpa).Groups[1].Value;

                var nameurlFormate = "https://api.weibo.com/2/users/show.json?access_token={0}&uid={1}";
                var nameurl        = string.Format(
                    nameurlFormate,
                    Uri.EscapeDataString(accesstoken),
                    Uri.EscapeDataString(openid));
                var nameResponse = await this._httpClient.GetAsync(nameurl, this.Request.CallCancelled);

                nameResponse.EnsureSuccessStatusCode();
                string nametxt = await nameResponse.Content.ReadAsStringAsync();

                var namepa = "\"name\":\"(.+?)\"";
                var name   = Regex.Match(nametxt, namepa).Groups[1].Value;
                //todo

                var context = new SinaAuthenticatedContext(this.Context, accesstoken, openid, name);

                var identity = new ClaimsIdentity(this.Options.AuthenticationType);

                if (!string.IsNullOrEmpty(context.OpenId))
                {
                    identity.AddClaim(
                        new Claim(
                            "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
                            context.OpenId,
                            "http://www.w3.org/2001/XMLSchema#string",
                            this.Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.Name))
                {
                    identity.AddClaim(
                        new Claim(
                            "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
                            context.Name,
                            "http://www.w3.org/2001/XMLSchema#string",
                            this.Options.AuthenticationType));
                }
                await this.Options.Provider.Authenticated(context);

                authenticationTicket = new AuthenticationTicket(identity, properties);
            }
            return(authenticationTicket);
        }
 public Task Authenticated(SinaAuthenticatedContext context)
 {
     return(this.OnAuthenticated(context));
 }