protected override async Task <AuthenticationTicket> AuthenticateCoreAsync() { AuthenticationProperties properties = null; try { string code = null; string csrf_state = null; //obtaining the shortened state value from the cookie HttpCookie csrfStateCookie = HttpContext.Current.Request.Cookies["csrf_state"]; string originalStateString = csrfStateCookie.Value; 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) { csrf_state = values[0]; } //verify sent and recieved state parameters for CSRF if (csrf_state != originalStateString) { return(null); } //Obtain the original state value from cookie HttpCookie stateCookie = HttpContext.Current.Request.Cookies["state"]; string state = stateCookie.Value; properties = Options.StateDataFormat.Unprotect(state); if (properties == null) { return(null); } 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.AppKey) + "&client_secret=" + Uri.EscapeDataString(Options.AppSecret); HttpRequestMessage request = new HttpRequestMessage(); HttpResponseMessage tokenResponse = await _httpClient.PostAsync(TokenEndpoint + "?" + tokenRequest, request.Content, Request.CallCancelled); tokenResponse.EnsureSuccessStatusCode(); string text = await tokenResponse.Content.ReadAsStringAsync(); JObject tokenResult = JObject.Parse(text); JToken access_token = tokenResult["access_token"] as JToken; string accessToken = access_token.ToString(); //set expiration time of the access token to 2 months string expires = "5183999"; HttpResponseMessage response = await _httpClient.GetAsync( ApiEndpoint + "?access_token=" + Uri.EscapeDataString(accessToken), Request.CallCancelled); response.EnsureSuccessStatusCode(); text = await response.Content.ReadAsStringAsync(); JObject user = JObject.Parse(text); var context = new DropboxAuthenticatedContext(Context, user, accessToken, expires); context.Identity = new ClaimsIdentity( Options.AuthenticationType, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType); if (!string.IsNullOrEmpty(context.UId)) { context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, context.UId, XmlSchemaString, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.DisplayName)) { context.Identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, context.DisplayName, XmlSchemaString, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.Email)) { context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email, XmlSchemaString, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.Country)) { context.Identity.AddClaim(new Claim(ClaimTypes.Country, context.Country, XmlSchemaString, Options.AuthenticationType)); } if (!string.IsNullOrEmpty(context.ReferralLink)) { context.Identity.AddClaim(new Claim("urn:dropbox:referral_link", context.ReferralLink, 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(DropboxAuthenticatedContext context) { return(OnAuthenticated(context)); }
public virtual Task Authenticated(DropboxAuthenticatedContext context) { return OnAuthenticated(context); }