lodash | title | name | image | tags | snippets | alias | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
true |
ASP.NET (OWIN) Tutorial |
ASP.NET (OWIN) |
/media/platforms/asp.png |
|
|
|
<%= include('../_includes/_package', { pkgRepo: 'auth0-aspnet-owin', pkgBranch: 'master', pkgPath: 'examples/basic-mvc-sample', pkgFilePath: 'examples/basic-mvc-sample/BasicMvcSample/Web.config', pkgType: 'replace' + account.clientParam }) %>
This tutorial explains how to integrate Auth0 with an ASP.NET application (of any kind: WebForms, MVC and even Web API) that uses the ASP.NET 4.5 Owin infrastructure.
Use the NuGet Package Manager (Tools -> Library Package Manager -> Package Manager Console) to install the Auth0-ASPNET-Owin package, running the command:
${snippet(meta.snippets.dependencies)}
After authenticating the user on Auth0, we will do a POST to your website. The first POST will be to the built-in OWIN route "/signin-auth0" (For security purposes, you have to register this URL on the Application Settings section on Auth0 Dashboard). After that is successful, it will redirect again to "/Auth0Account/ExternalLoginCallback" (Please do not register this route on the dashboard).
http://localhost:PORT/signin-auth0
The NuGet package also created three settings on <appSettings>
. Replace those with the following settings:
<add key="auth0:ClientId" value="${account.clientId}" />
<add key="auth0:ClientSecret" value="${account.clientSecret}" />
<add key="auth0:Domain" value="${account.namespace}" />
${snippet(meta.snippets.setup)}
The nuget provides a simple controller (Auth0AccountController) to process the authentication response from Auth0. If you want to use your own controller, make sure you set the redirectPath
parameter. For example, in order to use the implementation provided by Visual Studio templates, use the following: redirectPath: "/Account/ExternalLoginCallback"
.
${lockSDK}
Once the user is successfully authenticated with the application, a ClaimsPrincipal
will be generated which can be accessed through the Current
property:
public ActionResult Index()
{
string email = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value;
}
The user profile is normalized regardless of where the user came from. We will always include these: user_id
, name
, email
, nickname
, and picture
. For more information about the user profile, see this article.
Congratulations!
You can use the declarative [Authorize]
, <location path='..'>
in web.config
or code-based checks like User.Identity.IsAuthenticated
.
To clear the cookie generated on login, use the HttpContext.GetOwinContext().Authentication.SignOut()
method.
Browse the sample on GitHub.