public MyStack() { // Create an Auth0 Client var myClient = new Auth0.Client("client", new Auth0.ClientArgs { AllowedLogoutUrls = { "https://example.com/logout", }, AllowedOrigins = { "https://example.com", }, AppType = "regular_web", Callbacks = { "https://example.com/auth/callback", }, JwtConfiguration = new Auth0.Inputs.ClientJwtConfigurationArgs { Alg = "RS256", }, }); // Export Client ID and Secret this.ClientID = myClient.ClientId; this.ClientSecret = myClient.ClientSecret; }
public async Task<IActionResult> Callback(string access_token, string id_token, string state) { if (Context.User.IsSignedIn()) { Context.Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationScheme); } var client = new Auth0.Client( _config.Get("Authentication:Auth0:ClientId"), _config.Get("Authentication:Auth0:ClientSecret"), _config.Get("Authentication:Auth0:Domain")); var profile = client.GetUserInfo(new TokenResult { AccessToken = access_token, IdToken = id_token }); //var externalIdentity = AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie); //if (externalIdentity == null) //{ // throw new Exception("Could not get the external identity. Please check your Auth0 configuration settings and ensure that " + // "you configured UseCookieAuthentication and UseExternalSignInCookie in the OWIN Startup class. " + // "Also make sure you are not calling setting the callbackOnLocationHash option on the JavaScript login widget."); //} //AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, CreateIdentity(externalIdentity)); var userCP = new ClaimsPrincipal( new ClaimsIdentity( new[] { new Claim(ClaimTypes.Name, profile.Name), new Claim("UserType", profile.ExtraProperties.First(x => x.Key == "allReadyUserType").Value.ToString()) }, CookieAuthenticationDefaults.AuthenticationScheme)); var userManager = (UserManager<ApplicationUser>)Context.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>)); var user = await userManager.FindByIdAsync(profile.UserId); if (user == null) { user = new ApplicationUser { UserName = profile.UserId, Email = profile.Email }; user.EmailConfirmed = true; await userManager.CreateAsync(user); await _dataAccess.AddUser(user); // CreateAsync doesn't seem to be persisting the user if (profile.ExtraProperties.Any(x => x.Key == "allReadyUserType")) { await userManager.AddClaimAsync(user, new Claim("UserType", profile.ExtraProperties.First(x => x.Key == "allReadyUserType").Value.ToString())); } } Context.Authentication.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, userCP); return Redirect("/"); }