public override Task Authenticated(GoogleOAuth2AuthenticatedContext context) { context.Identity.AddClaim(new Claim(Claims.ExternalAccessToken, context.AccessToken)); context.Identity.AddClaim(new Claim(Claims.ExternalExpiresIn, context.ExpiresIn.ToString())); return base.Authenticated(context); }
public Task Authenticated(GoogleOAuth2AuthenticatedContext context) { context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken)); //context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString())); //context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString())); return Task.FromResult<object>(null); }
public Task Authenticated(GoogleOAuth2AuthenticatedContext context) { context.Identity.AddClaim(new Claim("external_access_token", context.AccessToken)); var uri = new Uri(context.User["image"].Value<string>("url")); context.Identity.AddClaim(new Claim("picture_url", uri.GetLeftPart(UriPartial.Path))); return Task.FromResult<object>(null); }
public override Task Authenticated(GoogleOAuth2AuthenticatedContext context) { string avatarUrl = context.User .SelectToken("image.url") .ToString() .Replace("sz=50", "sz=240"); context.Identity.AddClaim( new Claim(OwinHelper.ClaimTypeAvatarUrl, avatarUrl)); return base.Authenticated(context); }
internal async static void GetClaimsPrincipalAuthenticatedExternalCookieForGoogle() { GoogleAuthProvider googleAuthProvider = new GoogleAuthProvider(); var gctx = new GoogleOAuth2AuthenticatedContext(new OwinContext(), new JObject(), "123456", "", ""); List<Claim> claims = new List<Claim>(); claims.Add(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "115247531399035464127}", null, "Google")); claims.Add(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname", "Darren", null, "Google")); claims.Add(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname", "Schwarz", null, "Google")); claims.Add(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "Darren Schwarz", null, "Google")); claims.Add(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", "*****@*****.**", null, "Google")); claims.Add(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/urn:google:profile", "https://plus.google.com/115247531399035464127}", null, "Google")); claims.Add(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/ExternalAccessToken", "ya29.5wE5cDqpwP_XAkoTVCYA7oL56869HCS3upwr-TU1QkBz06GJVYz0YQ4v20UAfqb3M8IqBCA", null, "Google")); gctx.Identity = new ClaimsIdentity(claims, "ExternalCookie", "name", "role"); await googleAuthProvider.Authenticated(gctx); ClaimsPrincipal principal = new ClaimsPrincipal(gctx.Identity); Thread.CurrentPrincipal = principal; ClaimsPrincipal.ClaimsPrincipalSelector = () => principal; }
public Task Authenticated(GoogleOAuth2AuthenticatedContext context) { context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken)); return Task.FromResult<object>(null); }
//G , F 驗證完畢 , 會透過 ASP.NET 預設的 http://localhost:1520/signin-google 進行導向。 //而導向完後,就會進入底下方法,並且於 context 紀錄登入過後的資訊 ( 也就是說,取得相關資訊的事情..MS都處理掉了.. ) public Task Authenticated(GoogleOAuth2AuthenticatedContext context) { //取得外部登入的存取 Token ,例如,取得存取 Google 帳號資訊的 Token context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken)); return Task.FromResult<object>(null); }
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; // Build up the body for the token request var body = new List <KeyValuePair <string, string> >(); body.Add(new KeyValuePair <string, string>("grant_type", "authorization_code")); body.Add(new KeyValuePair <string, string>("code", code)); body.Add(new KeyValuePair <string, string>("redirect_uri", redirectUri)); body.Add(new KeyValuePair <string, string>("client_id", Options.ClientId)); body.Add(new KeyValuePair <string, string>("client_secret", Options.ClientSecret)); // Request the token HttpResponseMessage tokenResponse = await _httpClient.PostAsync(TokenEndpoint, new FormUrlEncodedContent(body)); tokenResponse.EnsureSuccessStatusCode(); string text = await tokenResponse.Content.ReadAsStringAsync(); // Deserializes the token response JObject response = JObject.Parse(text); string accessToken = response.Value <string>("access_token"); if (string.IsNullOrWhiteSpace(accessToken)) { _logger.WriteWarning("Access token was not found"); return(new AuthenticationTicket(null, properties)); } // Get the Google user HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, UserInfoEndpoint); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); HttpResponseMessage graphResponse = await _httpClient.SendAsync(request, Request.CallCancelled); graphResponse.EnsureSuccessStatusCode(); text = await graphResponse.Content.ReadAsStringAsync(); JObject user = JObject.Parse(text); var context = new GoogleOAuth2AuthenticatedContext(Context, user, response); context.Identity = new ClaimsIdentity( Options.AuthenticationType, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType); if (!string.IsNullOrEmpty(context.Id)) { context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, context.Id, ClaimValueTypes.String, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.GivenName)) { context.Identity.AddClaim(new Claim(ClaimTypes.GivenName, context.GivenName, ClaimValueTypes.String, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.FamilyName)) { context.Identity.AddClaim(new Claim(ClaimTypes.Surname, context.FamilyName, ClaimValueTypes.String, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.Name)) { context.Identity.AddClaim(new Claim(ClaimTypes.Name, context.Name, ClaimValueTypes.String, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.Email)) { context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email, ClaimValueTypes.String, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.Profile)) { context.Identity.AddClaim(new Claim("urn:google:profile", context.Profile, ClaimValueTypes.String, Options.AuthenticationType)); } context.Properties = properties; await Options.Provider.Authenticated(context); return(new AuthenticationTicket(context.Identity, context.Properties)); } catch (Exception ex) { _logger.WriteError("Authentication failed", ex); return(new AuthenticationTicket(null, properties)); } }
public Task Authenticated(GoogleOAuth2AuthenticatedContext context) { context.Identity.AddClaim(new Claim(GenericNames.AUTHENTICATION_EXTERNAL_LOGIN, context.AccessToken)); return Task.FromResult<object>(null); }
/// <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(GoogleOAuth2AuthenticatedContext 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(GoogleOAuth2AuthenticatedContext context) { return OnAuthenticated(context); }