Пример #1
0
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            try
            {
                // Get the token from the header
                var tokenHeader = Request.Headers.Get("Authorization");
                var token       = tokenHeader.Replace("Bearer ", "");
                if (string.IsNullOrEmpty(token))
                {
                    return(null);
                }

                // Get the GitHub user
                var userDataRequestUsingAccessToken = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/user?access_token=" + Uri.EscapeDataString(token));
                userDataRequestUsingAccessToken.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage userResponse = await _httpClient.SendAsync(userDataRequestUsingAccessToken, Request.CallCancelled);

                userResponse.EnsureSuccessStatusCode();
                var text = await userResponse.Content.ReadAsStringAsync();

                JObject user = JObject.Parse(text);

                var context = new GitHubAuthenticatedContext(Context, user, token)
                {
                    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.Name))
                {
                    context.Identity.AddClaim(new Claim("urn:github:name", context.Name, XmlSchemaString, Options.AuthenticationType));
                }
                if (!string.IsNullOrEmpty(context.Link))
                {
                    context.Identity.AddClaim(new Claim("urn:github:url", context.Link, XmlSchemaString, Options.AuthenticationType));
                }

                await Options.Provider.Authenticated(context);

                return(new AuthenticationTicket(context.Identity, context.Properties));
            }
            catch (Exception ex)
            {
                _logger.WriteError(ex.Message);
            }
            return(new AuthenticationTicket(null, null));
        }
 public virtual Task Authenticated(GitHubAuthenticatedContext context)
 {
     return(OnAuthenticated(context));
 }