Skip to content

Commit 3d4f4a1

Browse files
authored
Merge pull request #234617 from jmprieur/jmprieur/aspnetQsCallsGraph
Update QS
2 parents d5ee94a + 06742cf commit 3d4f4a1

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

articles/active-directory/develop/includes/web-app/quickstart-aspnet.md

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ See [How the sample works](#how-the-sample-works) for an illustration.
2626
## Prerequisites
2727

2828
* An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
29-
* [Visual Studio 2019](https://visualstudio.microsoft.com/vs/)
29+
* [Visual Studio 2022](https://visualstudio.microsoft.com/vs/)
3030
* [.NET Framework 4.7.2+](https://dotnet.microsoft.com/download/visual-studio-sdks)
3131

3232
## Register and download the app
@@ -71,11 +71,11 @@ If you want to manually configure your application and code sample, use the foll
7171
3. Depending on the version of Visual Studio, you might need to right-click the project **AppModelv2-WebApp-OpenIDConnect-DotNet** and then select **Restore NuGet packages**.
7272
4. Open the Package Manager Console by selecting **View** > **Other Windows** > **Package Manager Console**. Then run `Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -r`.
7373

74-
5. Edit *Web.config* and replace the parameters `ClientId`, `Tenant`, and `redirectUri` with:
75-
```xml
76-
<add key="ClientId" value="Enter_the_Application_Id_here" />
77-
<add key="Tenant" value="Enter_the_Tenant_Info_Here" />
78-
<add key="redirectUri" value="https://localhost:44368/" />
74+
5. Edit *appsettings.json* and replace the parameters `ClientId`, `Tenant`, and `redirectUri` with:
75+
```json
76+
"ClientId" :"Enter_the_Application_Id_here" />
77+
"TenantId": "Enter_the_Tenant_Info_Here" />
78+
"RedirectUri" :"https://localhost:44368/" />
7979
```
8080
In that code:
8181

@@ -100,48 +100,30 @@ This section gives an overview of the code required to sign in users. This overv
100100
You can set up the authentication pipeline with cookie-based authentication by using OpenID Connect in ASP.NET with OWIN middleware packages. You can install these packages by running the following commands in Package Manager Console within Visual Studio:
101101

102102
```powershell
103-
Install-Package Microsoft.Owin.Security.OpenIdConnect
103+
Install-Package Microsoft.Identity.Web.Owin
104+
Install-Package Microsoft.Identity.Web.MicrosoftGraph
104105
Install-Package Microsoft.Owin.Security.Cookies
105-
Install-Package Microsoft.Owin.Host.SystemWeb
106106
```
107107

108108
### OWIN startup class
109109

110110
The OWIN middleware uses a *startup class* that runs when the hosting process starts. In this quickstart, the *startup.cs* file is in the root folder. The following code shows the parameters that this quickstart uses:
111111

112112
```csharp
113-
public void Configuration(IAppBuilder app)
114-
{
115-
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
116-
117-
app.UseCookieAuthentication(new CookieAuthenticationOptions());
118-
app.UseOpenIdConnectAuthentication(
119-
new OpenIdConnectAuthenticationOptions
120-
{
121-
// Sets the client ID, authority, and redirect URI as obtained from Web.config
122-
ClientId = clientId,
123-
Authority = authority,
124-
RedirectUri = redirectUri,
125-
// PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it's using the home page
126-
PostLogoutRedirectUri = redirectUri,
127-
Scope = OpenIdConnectScope.OpenIdProfile,
128-
// ResponseType is set to request the code id_token, which contains basic information about the signed-in user
129-
ResponseType = OpenIdConnectResponseType.CodeIdToken,
130-
// ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application
131-
// To only allow users from a single organization, set ValidateIssuer to true and the 'tenant' setting in Web.config to the tenant name
132-
// To allow users from only a list of specific organizations, set ValidateIssuer to true and use the ValidIssuers parameter
133-
TokenValidationParameters = new TokenValidationParameters()
134-
{
135-
ValidateIssuer = false // Simplification (see note below)
136-
},
137-
// OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to the OnAuthenticationFailed method
138-
Notifications = new OpenIdConnectAuthenticationNotifications
139-
{
140-
AuthenticationFailed = OnAuthenticationFailed
141-
}
142-
}
143-
);
144-
}
113+
public void Configuration(IAppBuilder app)
114+
{
115+
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
116+
117+
app.UseCookieAuthentication(new CookieAuthenticationOptions());
118+
OwinTokenAcquirerFactory factory = TokenAcquirerFactory.GetDefaultInstance<OwinTokenAcquirerFactory>();
119+
120+
app.AddMicrosoftIdentityWebApp(factory);
121+
factory.Services
122+
.Configure<ConfidentialClientApplicationOptions>(options => { options.RedirectUri = "https://localhost:44368/"; })
123+
.AddMicrosoftGraph()
124+
.AddInMemoryTokenCaches();
125+
factory.Build();
126+
}
145127
```
146128

147129
|Where | Description |
@@ -155,10 +137,6 @@ public void Configuration(IAppBuilder app)
155137
| `TokenValidationParameters` | A list of parameters for token validation. In this case, `ValidateIssuer` is set to `false` to indicate that it can accept sign-ins from any personal, work, or school account type. |
156138
| `Notifications` | A list of delegates that can be run on `OpenIdConnect` messages. |
157139

158-
159-
> [!NOTE]
160-
> Setting `ValidateIssuer = false` is a simplification for this quickstart. In real applications, validate the issuer. See the samples to understand how to do that.
161-
162140
### Authentication challenge
163141

164142
You can force a user to sign in by requesting an authentication challenge in your controller:
@@ -182,6 +160,24 @@ public void SignIn()
182160

183161
You can protect a controller or controller actions by using the `[Authorize]` attribute. This attribute restricts access to the controller or actions by allowing only authenticated users to access the actions in the controller. An authentication challenge will then happen automatically when an unauthenticated user tries to access one of the actions or controllers decorated by the `[Authorize]` attribute.
184162

163+
### Call Microsoft Graph from the controller
164+
165+
You can call Microsoft Graph from the controller by getting the instance of GraphServiceClient using the `GetGraphServiceClient` extension method on the controller, like in the following code:
166+
167+
```csharp
168+
try
169+
{
170+
var me = await this.GetGraphServiceClient().Me.Request().GetAsync();
171+
ViewBag.Username = me.DisplayName;
172+
}
173+
catch (ServiceException graphEx) when (graphEx.InnerException is MicrosoftIdentityWebChallengeUserException)
174+
{
175+
HttpContext.GetOwinContext().Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
176+
return View();
177+
}
178+
```
179+
180+
185181
[!INCLUDE [Help and support](../../../../../includes/active-directory-develop-help-support-include.md)]
186182

187183
## Next steps

0 commit comments

Comments
 (0)