コード例 #1
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);
                }

                if (!ValidateCorrelationId(properties, _logger))
                {
                    return(new AuthenticationTicket(null, properties));
                }

                string requestPrefix = Request.Scheme + Uri.SchemeDelimiter + Request.Host;
                string redirectUri   = requestPrefix + Request.PathBase + Options.CallbackPath;

                string tokenRequestParameters = "client_id=" + Uri.EscapeDataString(Options.ClientId) +
                                                "&client_secret=" + Uri.EscapeDataString(Options.ClientSecret) +
                                                "&grant_type=authorization_code" +
                                                "&redirect_uri=" + redirectUri +
                                                "&code=" + Uri.EscapeDataString(code);

                HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(TokenEndpoint);

                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[]        data     = encoding.GetBytes(tokenRequestParameters);

                httpWReq.Method        = "POST";
                httpWReq.ContentType   = "application/x-www-form-urlencoded";
                httpWReq.ContentLength = data.Length;
                httpWReq.Timeout       = System.Threading.Timeout.Infinite;

                using (Stream stream = httpWReq.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                //Send token request as a POST message
                HttpWebResponse response       = (HttpWebResponse)httpWReq.GetResponse();
                string          responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

                //set expiration of the access token to 60 days
                string expires = "5183999";

                JObject JResponse    = JObject.Parse(responseString);
                JToken  JAccessToken = JResponse["access_token"];
                JToken  JUser        = JResponse["user"];

                string  accessToken = JAccessToken.ToString();
                JObject user        = JUser as JObject;

                var context = new InstagramAuthenticatedContext(Context, user, accessToken, expires);
                context.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.UserName))
                {
                    context.Identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, context.UserName, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.FullName))
                {
                    context.Identity.AddClaim(new Claim(ClaimTypes.Name, context.FullName, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.ProfilePicture))
                {
                    context.Identity.AddClaim(new Claim("urn:instagram:profilepicture", context.ProfilePicture, 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));
        }
 public virtual Task Authenticated(InstagramAuthenticatedContext context)
 {
     return(OnAuthenticated(context));
 }