This example shows how to add login/logout and extract user profile information from claims.
You can read a quickstart for this sample here.
- .NET SDK (.NET Core 3.1 or .NET 5.0+)
-
Ensure that you have replaced the
appsettings.json
file with the values for your Auth0 account. -
Run the application from the command line:
dotnet run
- Go to
http://localhost:3000
in your web browser to view the website.
In order to run the example with Docker you need to have Docker installed.
To build the Docker image and run the project inside a container, run the following command in a terminal, depending on your operating system:
# Mac
sh exec.sh
# Windows (using Powershell)
.\exec.ps1
public void ConfigureServices(IServiceCollection services)
{
services.AddAuth0WebAppAuthentication(options => {
options.Domain = Configuration["Auth0:Domain"];
options.ClientId = Configuration["Auth0:ClientId"];
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseAuthentication();
app.UseAuthorization();
...
}
public async Task Login(string returnUrl = "/")
{
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri(returnUrl)
.Build();
await HttpContext.ChallengeAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
}
[Authorize]
public IActionResult Profile()
{
return View(new UserProfileViewModel()
{
Name = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value,
EmailAddress = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value,
ProfileImage = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value
});
}
[Authorize]
public async Task Logout()
{
var authenticationProperties = new LogoutAuthenticationPropertiesBuilder()
// Indicate here where Auth0 should redirect the user after a logout.
// Note that the resulting absolute Uri must be whitelisted in the
// **Allowed Logout URLs** settings for the client.
.WithRedirectUri(Url.Action("Index", "Home"))
.Build();
await HttpContext.SignOutAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}