public async Task <List <Claim> > RequestAndValidateTokenAsync(string code) { List <Claim> claims = null; // Request var response = await RequestTokenAsync(code); // Validate var tempAuthentication = new TempAuthentication(); var tempState = await tempAuthentication.GetTempStateAsync(); tempAuthentication.SignOut(); if (!string.IsNullOrWhiteSpace(response.IdentityToken)) { claims = ValidateToken(response.IdentityToken, tempState.Item2); if (!string.IsNullOrWhiteSpace(response.AccessToken)) { //claims.AddRange(await GetUserInfoClaimsAsync(response.AccessToken)); claims.Add(new Claim("access_token", response.AccessToken)); claims.Add(new Claim("expires_at", (DateTime.UtcNow.ToEpochTime() + response.ExpiresIn).ToDateTimeFromEpoch().ToString())); } if (!string.IsNullOrWhiteSpace(response.RefreshToken)) { claims.Add(new Claim("refresh_token", response.RefreshToken)); } } return(claims); }
public string CreateAuthorizeUrl() { string nonce = Guid.NewGuid().ToString("N"); string state = Guid.NewGuid().ToString("N"); var tempAuthentication = new TempAuthentication(); tempAuthentication.SetTempState(state, nonce); return(string.Format(OpenIdSettings.Settings.AuthorizeUrlFormat, OpenIdConfig.Config.AuthorizationEndpoint, HttpUtility.UrlEncode(OpenIdSettings.Settings.ClientId), HttpUtility.UrlEncode(OpenIdSettings.Settings.ResponseType), HttpUtility.UrlEncode(OpenIdSettings.Settings.Scope), HttpUtility.UrlEncode(OpenIdSettings.Settings.RedirectUri), HttpUtility.UrlEncode(state), HttpUtility.UrlEncode(nonce))); }
public async Task <ActionResult> Index(string code, string state) { var tempAuthentiction = new TempAuthentication(); var tempState = await tempAuthentiction.GetTempStateAsync(); if (!state.Equals(tempState.Item1, StringComparison.Ordinal)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "state invalid")); } var token = new Token(); var claims = await token.RequestAndValidateTokenAsync(code); var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); Request.GetOwinContext().Authentication.SignIn(id); return(RedirectToAction("Index", "Home")); }