Пример #1
0
 //
 public override Task Authenticated(LinkedInAuthenticatedContext context)
 {
     try
     {
         addClaim(context.Identity, "urn:tokens:linkedin:accesstoken", context.AccessToken);
         //
         if (context.Identity.HasClaim(_ => _.Type == "urn:linkedin:lastname") == false)
         {
             addClaim(context.Identity, "urn:tokens:linkedin:familyname", getValue(context.User, "last-name"));
         }
         if (context.Identity.HasClaim(_ => _.Type == "urn:linkedin:firstname") == false)
         {
             addClaim(context.Identity, "urn:tokens:linkedin:givenname", getValue(context.User, "first-name"));
         }
         //addClaim(context.Identity, "urn:tokens:linkedin:avatarurl", ""); // $@"https://media.licdn.com/media/p/7/000/1b2/0a9/{context.Id}.jpg"
         addClaim(context.Identity, "urn:tokens:linkedin:company", getValue(context.User, "company"));
     }
     catch (Exception ex) { Logger.Error(ex, LogCategory.Claims, "There was an issue reading the information from the Facebook Authentication context."); }
     //
     return(base.Authenticated(context));
 }
Пример #2
0
 public override Task Authenticated(LinkedInAuthenticatedContext context)
 {
     context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken));
     return(Task.FromResult <object>(null));
 }
Пример #3
0
 public override Task Authenticated(LinkedInAuthenticatedContext context)
 {
     context.Identity.AddClaim(
         new Claim(ServiceClaimTypes.ProviderAccessToken, context.AccessToken));
     return(base.Authenticated(context));
 }
Пример #4
0
 public Task ReturnEndpoint(LinkedInAuthenticatedContext context)
 {
     return(Task.FromResult <object>(null));
 }
Пример #5
0
 public void ApplyRedirect(LinkedInAuthenticatedContext context)
 {
     context.Response.Redirect(context.Properties.RedirectUri);
 }
        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(properties, _logger))
                {
                    return(new AuthenticationTicket(null, properties));
                }

                string requestPrefix = Request.Scheme + "://" + Request.Host;
                string redirectUri   = requestPrefix + Request.PathBase + Options.CallbackPath;


                string tokenRequest = "grant_type=authorization_code" +
                                      "&code=" + Uri.EscapeDataString(code) +
                                      "&redirect_uri=" + Uri.EscapeDataString(redirectUri) +
                                      "&client_id=" + Uri.EscapeDataString(Options.AppId) +
                                      "&client_secret=" + Uri.EscapeDataString(Options.AppSecret);

                HttpResponseMessage tokenResponse = await _httpClient.GetAsync(TokenEndpoint + "?" + tokenRequest, Request.CallCancelled);

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

                JObject form = JObject.Parse(text);


                JToken expires     = null;
                JToken accessToken = null;

                foreach (var x in form)
                {
                    if (x.Key == "expires_in")
                    {
                        expires = x.Value;
                    }

                    else if (x.Key == "access_token")
                    {
                        accessToken = x.Value;
                    }
                }

                HttpResponseMessage graphResponse = await _httpClient.GetAsync(
                    ApiEndpoint + "?oauth2_access_token=" + Uri.EscapeDataString(accessToken.ToString()), Request.CallCancelled);

                graphResponse.EnsureSuccessStatusCode();
                text = await graphResponse.Content.ReadAsStringAsync();

                string formattedString1 = text.Substring(56);

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(formattedString1);

                string  jsontext = JsonConvert.SerializeXmlNode(doc);
                string  final    = jsontext.Substring(10);
                string  final2   = final.Substring(0, final.Length - 1);
                JObject user     = JObject.Parse(final2);

                var context = new LinkedInAuthenticatedContext(Context, user, accessToken.ToString(), expires.ToString());
                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.Email))
                {
                    context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.FirstName))
                {
                    context.Identity.AddClaim(new Claim("urn:linkedin:firstname", context.FirstName, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.LastName))
                {
                    context.Identity.AddClaim(new Claim("urn:linkedin:lastname", context.LastName, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.Url))
                {
                    context.Identity.AddClaim(new Claim("urn:linkedin:link", context.Url, 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));
        }