static async Task<int> AsyncMain(string[] args) { Console.WriteLine("Press ENTER to call the API."); Console.ReadLine(); // Get a token using the Authentication client var client = new AuthenticationApiClient(new Uri("https://{DOMAIN}")); var token = await client.Authenticate(new AuthenticationRequest { ClientId = "{CLIENT_ID}", Connection = "Username-Password-Authentication", Username = "******", Password = "******", Scope = "openid profile" }); // Create a new HttpClient, and set the Auth header to the token we obtained var apiClient = new HttpClient(); apiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.IdToken); // Call the API, and extract the response var response = await apiClient.GetAsync("http://localhost:25100/api/sample"); var content = await response.Content.ReadAsAsync<IEnumerable<ClaimItem>>(); Console.WriteLine("Call complete. Data received:"); // Write all the claims received from the API to the console foreach (var item in content) Console.WriteLine(" > {0}: {1}", item.Type, item.Value); Console.ReadLine(); // Return A-OK return 0; }
public override async Task ProcessRequestAsync(HttpContext context) { AuthenticationApiClient client = new AuthenticationApiClient( new Uri(string.Format("https://{0}", ConfigurationManager.AppSettings["auth0:Domain"]))); var token = await client.ExchangeCodeForAccessTokenAsync(new ExchangeCodeRequest { ClientId = ConfigurationManager.AppSettings["auth0:ClientId"], ClientSecret = ConfigurationManager.AppSettings["auth0:ClientSecret"], AuthorizationCode = context.Request.QueryString["code"], RedirectUri = context.Request.Url.ToString() }); var profile = await client.GetUserInfoAsync(token.AccessToken); var user = new List<KeyValuePair<string, object>> { new KeyValuePair<string, object>("name", profile.UserName ?? profile.Email), new KeyValuePair<string, object>("email", profile.Email), new KeyValuePair<string, object>("family_name", profile.LastName), new KeyValuePair<string, object>("given_name", profile.FirstName), new KeyValuePair<string, object>("nickname", profile.NickName), new KeyValuePair<string, object>("picture", profile.Picture), new KeyValuePair<string, object>("user_id", profile.UserId), new KeyValuePair<string, object>("id_token", token.IdToken), new KeyValuePair<string, object>("access_token", token.AccessToken), new KeyValuePair<string, object>("refresh_token", token.RefreshToken), new KeyValuePair<string, object>("connection", profile.Identities.First().Connection), new KeyValuePair<string, object>("provider", profile.Identities.First().Provider) }; // NOTE: Uncomment the following code in order to include claims from associated identities //profile.Identities.ToList().ForEach(i => //{ // user.Add(new KeyValuePair<string, object>(i.Connection + ".access_token", i.AccessToken)); // user.Add(new KeyValuePair<string, object>(i.Connection + ".provider", i.Provider)); // user.Add(new KeyValuePair<string, object>(i.Connection + ".user_id", i.UserId)); //}); // NOTE: uncomment this if you send roles // user.Add(new KeyValuePair<string, object>(ClaimTypes.Role, profile.ExtraProperties["roles"])); // NOTE: this will set a cookie with all the user claims that will be converted // to a ClaimsPrincipal for each request using the SessionAuthenticationModule HttpModule. // You can choose your own mechanism to keep the user authenticated (FormsAuthentication, Session, etc.) FederatedAuthentication.SessionAuthenticationModule.CreateSessionCookie(user); if (context.Request.QueryString["state"] != null && context.Request.QueryString["state"].StartsWith("ru=")) { var state = HttpUtility.ParseQueryString(context.Request.QueryString["state"]); context.Response.Redirect(state["ru"], true); } context.Response.Redirect("/"); }
public AccountController() { auth0 = new AuthenticationApiClient(new System.Uri("https://" + System.Configuration.ConfigurationManager.AppSettings["auth0:Domain"])); }