public async Task <ActionResult> RefreshToken() { var principal = User as ClaimsPrincipal; var refreshToken = principal.FindFirst("refresh_token"); var info = new AppContainer { Message = refreshToken == null ? "You don't have a refresh token" : null }; if (info.Message == null) { HttpContent content = new [] { "grant_type", "redirect_uri", "refresh_token" } .ToOAuthFormat(new [] { "refresh_token", DefaultClientConfiguration.CurrentOAuthConfig.CallbackUrl, refreshToken.Value }); HttpClient client = new HttpClient() .SetBasicAuth(DefaultClientConfiguration.CurrentOAuthConfig.ClientId, DefaultClientConfiguration.CurrentOAuthConfig.ClientSecret); var response = await client.PostAsync(EndpointPaths.GetTokenEndpointUri(DefaultClientConfiguration.CurrentOAuthConfig), content); var tokenDetails = await response.Content.ReadAsStringAsync(); UpdateCookie(JsonConvert.DeserializeObject <TokenResponse>(tokenDetails)); } return(RedirectToAction("Index", info)); }
private string CreateCodeFlowUrl(string state, string nonce) { var model = DefaultClientConfiguration.CurrentOAuthConfig; var requestParams = new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("response_type", "code"), new KeyValuePair <string, string>("client_id", model.ClientId), new KeyValuePair <string, string>("scope", model.Scopes), new KeyValuePair <string, string>("redirect_uri", model.CallbackUrl), new KeyValuePair <string, string>("state", state), new KeyValuePair <string, string>("nonce", nonce), new KeyValuePair <string, string>("login_hint", "tenant:" + model.Tenant), }; string queryString = string.Join("&", requestParams.Select(p => string.Format("{0}={1}", Uri.EscapeDataString(p.Key), Uri.EscapeDataString(p.Value ?? string.Empty))).ToArray()); return(string.Format("{0}?{1}", EndpointPaths.GetAuthorizeEndpointUri(model), queryString));/* + "?response_type=code&client_id=" + model.ClientId + "&scope=" + model.Scopes.Replace(" ", "+") + "&redirect_uri=" + encode(model.CallbackUrl) + "&state=" + state + "&nonce=" + nonce + "&login_hint=" + encode("tenant:" + model.Tenant);*/ }
public async Task <ActionResult> GetToken() { var code = Request.QueryString["code"]; var tempState = await GetTempStateAsync(); Request.GetOwinContext().Authentication.SignOut("TempState"); HttpContent content = new [] { "code", "grant_type", "redirect_uri" } .ToOAuthFormat(new [] { code, "authorization_code", DefaultClientConfiguration.CurrentOAuthConfig.CallbackUrl }); HttpClient client = new HttpClient() .SetBasicAuth(DefaultClientConfiguration.CurrentOAuthConfig.ClientId, DefaultClientConfiguration.CurrentOAuthConfig.ClientSecret); var response = await client.PostAsync(EndpointPaths.GetTokenEndpointUri(DefaultClientConfiguration.CurrentOAuthConfig), content); var tokenDetails = await response.Content.ReadAsStringAsync(); var decoded = JsonConvert.DeserializeObject <TokenResponse>(tokenDetails); decoded.raw = tokenDetails; await ValidateResponseAndSignInAsync(decoded, tempState.Item2); return(View("Token", decoded)); }