public async Task <ActionResult> Login(LoginViewModel vm, string returnUrl = null) { if (ModelState.IsValid) { try { AuthenticationApiClient client = new AuthenticationApiClient( new Uri($"https://{ConfigurationManager.AppSettings["auth0:Domain"]}/")); var result = await client.AuthenticateAsync(new AuthenticationRequest { ClientId = ConfigurationManager.AppSettings["auth0:ClientId"], Scope = "openid", Connection = "Database-Connection", // Specify the correct name of your DB connection Username = vm.EmailAddress, Password = vm.Password }); // Get user info from token var user = await client.GetTokenInfoAsync(result.IdToken); // Create claims principal var claimsIdentity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, user.UserId), new Claim(ClaimTypes.Name, user.FullName ?? user.Email), new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string") }, DefaultAuthenticationTypes.ApplicationCookie); // Sign user into cookie middleware AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = false }, claimsIdentity); return(RedirectToLocal(returnUrl)); } catch (Exception e) { ModelState.AddModelError("", e.Message); } } return(View(vm)); }
public async Task Can_obtain_token_info() { var authenticationApiClient = new AuthenticationApiClient(new Uri(GetVariable("AUTH0_AUTHENTICATION_API_URL"))); // First get the access token var token = await authenticationApiClient.GetAccessTokenAsync(new AccessTokenRequest { ClientId = GetVariable("AUTH0_CLIENT_ID"), Connection = "google-oauth2", AccessToken = accessToken, Scope = "openid" }); // Get the user info var user = await authenticationApiClient.GetTokenInfoAsync(token.IdToken); user.Should().NotBeNull(); user.Email.Should().NotBeNull(); }
public static async Task AuthenticationApiMainAsync(string[] args) { try { string token = ""; var handler = new HttpClientHandler { Proxy = new WebProxy() }; var api = new AuthenticationApiClient("jerrie.auth0.com"); var tokenInfo = await api.GetTokenInfoAsync(token); Console.WriteLine(tokenInfo.Email); } catch (Exception ex) { throw; } }
public async Task <IActionResult> Login(LoginViewModel vm, string returnUrl = null) { if (ModelState.IsValid) { try { AuthenticationApiClient client = new AuthenticationApiClient(new Uri($"https://{_auth0Settings.Domain}/")); var result = await client.AuthenticateAsync(new AuthenticationRequest { ClientId = _auth0Settings.ClientId, Scope = "openid", Connection = "Database-Connection", // Specify the correct name of your DB connection Username = vm.EmailAddress, Password = vm.Password }); // Get user info from token var user = await client.GetTokenInfoAsync(result.IdToken); // Create claims principal var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, user.UserId), new Claim(ClaimTypes.Name, user.FullName) }, CookieAuthenticationDefaults.AuthenticationScheme)); // Sign user into cookie middleware await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal); return(RedirectToLocal(returnUrl)); } catch (Exception e) { ModelState.AddModelError("", e.Message); } } return(View(vm)); }