page_type: sample languages:
This sample demonstrates a ASP.NET web app application that authenticates users against Azure AD.
From your shell or command line:
git clone https://github.com/AzureADQuickStarts/AppModelv2-WebApp-OpenIDConnect-DotNet.git
cd AppModelv2-WebApp-OpenIDConnect-DotNet
or download and extract the repository .zip file.
:warning: To avoid path length limitations on Windows, we recommend cloning into a directory near the root of your drive.
There is one project in this sample. To register it, you can:
As a first step you'll need to:
Quickstart-AspNetWebApp
.https://localhost:44368/
.
Note that there are more than one redirect URIs used in this sample. You'll need to add them from the Authentication tab later after the app has been created successfully.
https://localhost:44368/signin-oidc
https://localhost:44368/signout-oidc
.Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
AppModelv2-WebApp-OpenIDConnect-DotNet\Web.config
file.ClientId
and replace the existing value with the application ID (clientId) of the Quickstart-AspNetWebApp
application copied from the Azure portal.Tenant
and replace the existing value with your Azure AD tenant ID.For Visual Studio Users
Clean the solution, rebuild the solution, and run it.
https://localhost:44368
.Select the Sign in button on the top right corner. When the user signs-in for the first time , a consent screen is presented with required permissions, select Accept.
Click on See Your Claims link, you will see claims from the signed-in user's token.
:information_source: Did the sample not work for you as expected? Then please reach out to us using the GitHub Issues page.
Were we successful in addressing your learning objective? Do consider taking a moment to share your experience with us.
In Startup.cs
, Configuration method configures OWIN to use OpenIdConnect as below:
public void Configuration(IAppBuilder app)
{
/// ...
OwinTokenAcquirerFactory factory = TokenAcquirerFactory.GetDefaultInstance<OwinTokenAcquirerFactory>();
app.AddMicrosoftIdentityWebApp(factory);
factory.Services
.Configure<ConfidentialClientApplicationOptions>(options => { options.RedirectUri = "https://localhost:44368/"; })
.AddMicrosoftGraph()
.AddInMemoryTokenCaches();
factory.Build();
}
HomeController.cs
contains SignIn and SignOut methods as following:
public class HomeController : Controller
{
...
public void SignIn()
{
if (!Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties { RedirectUri = "/" },
OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
}
public void SignOut()
{
HttpContext.GetOwinContext().Authentication.SignOut(
OpenIdConnectAuthenticationDefaults.AuthenticationType,
CookieAuthenticationDefaults.AuthenticationType);
}
}
ClaimsController
shows how to access the claims in the ID token
public ActionResult Index()
{
var userClaims = User.Identity as System.Security.Claims.ClaimsIdentity;
// You get the users first and last name below:
ViewBag.Name = userClaims?.FindFirst("name")?.Value;
// The subject/ NameIdentifier claim can be used to uniquely identify the user across the web
ViewBag.Subject = userClaims?.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
// TenantId is the unique Tenant Id - which represents an organization in Azure AD
ViewBag.TenantId = userClaims?.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid")?.Value;
}
It also shows how to call Microsoft Graph, with incremental consent (the user will need to consent to more scopes if needed.
// You can also call Microsoft Graph (with incremental consent)
try
{
var me = await this.GetGraphServiceClient().Me.GetAsync();
ViewBag.Username = me.DisplayName;
}
catch (ServiceException graphEx) when (graphEx.InnerException is MicrosoftIdentityWebChallengeUserException)
{
HttpContext.GetOwinContext().Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
return View();
}
For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
Use Stack Overflow to get support from the community.
Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.
Make sure that your questions or comments are tagged with [azure-active-directory
azure-ad-b2c
ms-identity
adal
msal
].
If you find a bug in the sample, raise the issue on GitHub Issues.
To provide feedback on or suggest features for Azure Active Directory, visit User Voice page.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.