protected override async Task <AuthenticationTicket> GetUserInformationAsync(AuthenticationProperties properties, TokenResponse tokens) { // Get the Google user HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); HttpResponseMessage graphResponse = await Backchannel.SendAsync(request, Context.RequestAborted); graphResponse.EnsureSuccessStatusCode(); var text = await graphResponse.Content.ReadAsStringAsync(); JObject user = JObject.Parse(text); var context = new GoogleAuthenticatedContext(Context, Options, user, tokens); var identity = new ClaimsIdentity( Options.AuthenticationScheme, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType); if (!string.IsNullOrEmpty(context.Id)) { identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, context.Id, ClaimValueTypes.String, Options.AuthenticationScheme)); } if (!string.IsNullOrEmpty(context.GivenName)) { identity.AddClaim(new Claim(ClaimTypes.GivenName, context.GivenName, ClaimValueTypes.String, Options.AuthenticationScheme)); } if (!string.IsNullOrEmpty(context.FamilyName)) { identity.AddClaim(new Claim(ClaimTypes.Surname, context.FamilyName, ClaimValueTypes.String, Options.AuthenticationScheme)); } if (!string.IsNullOrEmpty(context.Name)) { identity.AddClaim(new Claim(ClaimTypes.Name, context.Name, ClaimValueTypes.String, Options.AuthenticationScheme)); } if (!string.IsNullOrEmpty(context.Email)) { identity.AddClaim(new Claim(ClaimTypes.Email, context.Email, ClaimValueTypes.String, Options.AuthenticationScheme)); } if (!string.IsNullOrEmpty(context.Profile)) { identity.AddClaim(new Claim("urn:google:profile", context.Profile, ClaimValueTypes.String, Options.AuthenticationScheme)); } context.Properties = properties; context.Principal = new ClaimsPrincipal(identity); await Options.Notifications.Authenticated(context); return(new AuthenticationTicket(context.Principal, context.Properties, context.Options.AuthenticationScheme)); }
internal static async Task OnAuthenticated(GoogleAuthenticatedContext context) { if (context.Principal != null) { Helpers.ThrowIfConditionFailed(() => context.AccessToken == "ValidAccessToken", "Access token is not valid"); Helpers.ThrowIfConditionFailed(() => context.RefreshToken == "ValidRefreshToken", "Refresh token is not valid"); Helpers.ThrowIfConditionFailed(() => context.Email == "*****@*****.**", "Email is not valid"); Helpers.ThrowIfConditionFailed(() => context.Id == "106790274378320830963", "Id is not valid"); Helpers.ThrowIfConditionFailed(() => context.FamilyName == "AspnetvnextTest", "FamilyName is not valid"); Helpers.ThrowIfConditionFailed(() => context.Name == "AspnetvnextTest AspnetvnextTest", "Name is not valid"); Helpers.ThrowIfConditionFailed(() => context.ExpiresIn.Value == TimeSpan.FromSeconds(1200), "ExpiresIn is not valid"); Helpers.ThrowIfConditionFailed(() => context.User != null, "User object is not valid"); context.Principal.Identities.First().AddClaim(new Claim("ManageStore", "false")); } await Task.FromResult(0); }
/// <summary> /// Invoked whenever Google succesfully authenticates a user. /// </summary> /// <param name="context">Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.</param> /// <returns>A <see cref="Task"/> representing the completed operation.</returns> public virtual Task Authenticated(GoogleAuthenticatedContext context) { return OnAuthenticated(context); }
/// <summary> /// Invoked whenever Google succesfully authenticates a user. /// </summary> /// <param name="context">Contains information about the login session as well as the user <see cref="System.Security.Claims.ClaimsIdentity"/>.</param> /// <returns>A <see cref="Task"/> representing the completed operation.</returns> public virtual Task Authenticated(GoogleAuthenticatedContext context) { return(OnAuthenticated(context)); }