Skip to content

Commit 0425e08

Browse files
Merge branch 'master' into lr-update-self-hosted-to-aspnetcore-2.2
2 parents 2174ebb + 17642bb commit 0425e08

10 files changed

+144
-68
lines changed

Diff for: resource-server/README.md

+36-8
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,50 @@ A typical resource-server requires a frontend and a backend application, so you
2121

2222
## Running This Example
2323

24-
**backend:**
24+
### Backend
2525

26-
Clone this repo and replace the okta configuration placeholders in the `appsettings.json` with your configuration values from the Okta Developer Console.
27-
You can see all the available configuration options in the [okta-aspnet GitHub](https://github.com/okta/okta-aspnet/blob/master/README.md).
28-
For step-by-step instructions, visit the Okta [ASP.NET Web API quickstart].
26+
Run the example with your preferred tool and write down the port of your Web API application to configure Okta afterwards.
2927

30-
> **NOTE:** The above command starts the resource server on port 8000. You can browse to `http://localhost:8000/api/messages` to ensure it has started. If you get a 401 HTTP error, it indicates that the resource server is up. You will need to pass an access token to access the resource, which will be done by the front-end below.
28+
> **NOTE:** This sample is using ASP.NET Core 2.2 which enforces HTTPS. This is a recommended practice for web applications. Check out [Enforce HTTPS in ASP.NET Core] for more details.
3129
32-
**front-end:**
30+
#### Run the Web API application from Visual Studio
3331

34-
If you want to use one of our front-end samples, open a new terminal window and run the [front-end sample project of your choice](Prerequisites). Once the front-end sample is running, you can navigate to http://localhost:8080 in your browser and log in to the front-end application. Once logged in, you can navigate to the "Messages" page to see the interaction with the resource server.
32+
If you run this project in Visual Studio it will start the resource server on ports 5000 for HTTP and 44314 for HTTPS. You can change this configuration in the `launchSettings.json`.
33+
You can browse to `https://localhost:44314/api/messages` to ensure it has started. If you get a 401 HTTP error, it indicates that the resource server is up. You will need to pass an access token to access the resource, which will be done by the front-end below.
34+
35+
#### Run the Web API application from dotnet CLI
36+
37+
If you run this project via the dotnet CLI it will start the resource server on ports 5000 for HTTP and 5001 for HTTPS. You can change this configuration in the `launchSettings.json`.
38+
39+
Navigate to the folder where the project file is located and type the following:
40+
41+
```dotnet run```
42+
43+
You can browse to `https://localhost:5001/api/messages` to ensure it has started. If you get a 401 HTTP error, it indicates that the resource server is up. You will need to pass an access token to access the resource, which will be done by the front-end below.
44+
45+
#### Trust the local dev certificate if necessary
46+
47+
If you’ve never run an ASP.NET Core 2.x application before, you may notice a strange error page come up warning you that the site is potentially unsafe.
48+
This is because ASP.NET Core creates an HTTPS development certificate for you as part of the first-run experience, but it still needs to be trusted. You can ignore the warning by clicking on Advanced and telling the browser that it’s okay to visit this site even though there is no certificate for it. Or you can trust the certificate to get rid of this warning, check out [Configuring HTTPS in ASP.NET Core across different platforms] for more details.
49+
50+
### Add your Okta configuration to the sample's appsettings
51+
52+
Replace the okta configuration placeholders in the `appsettings.json` with your configuration values from the [Okta Developer Console].
53+
You can see all the available configuration options in the [okta-aspnet GitHub](https://github.com/okta/okta-aspnet/blob/master/docs/aspnetcore-webapi.md#configuration-reference).
54+
For step-by-step instructions, visit the Okta [Protect your API endpoints guide]. The guide will walk you through adding Okta authentication to your API endpoints.
55+
56+
### Front-end
57+
58+
If you want to use one of our front-end samples, open a new terminal window and run the [front-end sample project of your choice](Prerequisites).
59+
Make sure to update the resource server URI configuration to use your configured resource-server port (for example `https://localhost:5001`).
60+
Once the front-end sample is running, you can navigate to `http://localhost:8080` in your browser and sign in to the front-end application. Once signed in, you can navigate to the "Messages" page to see the interaction with the resource server.
3561

3662

3763
[Implicit Flow]: https://developer.okta.com/authentication-guide/implementing-authentication/implicit
3864
[Okta Angular Sample Apps]: https://github.com/okta/samples-js-angular
3965
[Okta Vue Sample Apps]: https://github.com/okta/samples-js-vue
4066
[Okta React Sample Apps]: https://github.com/okta/samples-js-react
4167
[OIDC SPA Setup Instructions]: https://developer.okta.com/authentication-guide/implementing-authentication/implicit#1-setting-up-your-application
42-
[ASP.NET Core Web API quickstart]: https://developer.okta.com/quickstart/#/widget/dotnet/aspnetcore
68+
[Enforce HTTPS in ASP.NET Core]: https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-2.2&tabs=visual-studio
69+
[Configuring HTTPS in ASP.NET Core across different platforms]:https://devblogs.microsoft.com/aspnet/configuring-https-in-asp-net-core-across-different-platforms/
70+
[Protect your API endpoints guide]: https://developer.okta.com/guides/protect-your-api/aspnetcore/before-you-begin/

Diff for: resource-server/okta-aspnetcore-webapi-example.sln

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27130.2036
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.28729.10
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "okta-aspnetcore-webapi-example", "okta-aspnetcore-webapi-example\okta-aspnetcore-webapi-example.csproj", "{E664F22F-C256-401F-8E48-8680877C7873}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "okta-aspnetcore-webapi-example", "okta-aspnetcore-webapi-example\okta-aspnetcore-webapi-example.csproj", "{778066D7-71AE-4640-A442-9B6B4AE8CF2C}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1010
Debug|Any CPU = Debug|Any CPU
1111
Release|Any CPU = Release|Any CPU
1212
EndGlobalSection
1313
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14-
{E664F22F-C256-401F-8E48-8680877C7873}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15-
{E664F22F-C256-401F-8E48-8680877C7873}.Debug|Any CPU.Build.0 = Debug|Any CPU
16-
{E664F22F-C256-401F-8E48-8680877C7873}.Release|Any CPU.ActiveCfg = Release|Any CPU
17-
{E664F22F-C256-401F-8E48-8680877C7873}.Release|Any CPU.Build.0 = Release|Any CPU
14+
{778066D7-71AE-4640-A442-9B6B4AE8CF2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{778066D7-71AE-4640-A442-9B6B4AE8CF2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{778066D7-71AE-4640-A442-9B6B4AE8CF2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{778066D7-71AE-4640-A442-9B6B4AE8CF2C}.Release|Any CPU.Build.0 = Release|Any CPU
1818
EndGlobalSection
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE
2121
EndGlobalSection
2222
GlobalSection(ExtensibilityGlobals) = postSolution
23-
SolutionGuid = {012C08C2-C1EE-4614-B78A-4D2AC7568485}
23+
SolutionGuid = {A675EC44-E718-4243-913F-05C5CFA72B47}
2424
EndGlobalSection
2525
EndGlobal

Diff for: resource-server/okta-aspnetcore-webapi-example/Controllers/MessagesController.cs renamed to resource-server/okta-aspnetcore-webapi-example/Controllers/MessageController.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
namespace okta_aspnetcore_webapi_example.Controllers
1010
#pragma warning restore SA1300 // Element should begin with upper-case letter
1111
{
12-
[Produces("application/json")]
13-
[Authorize]
14-
public class MessagesController : Controller
12+
[Route("api/[controller]")]
13+
[ApiController]
14+
public class MessageController : ControllerBase
1515
{
16+
[Authorize]
1617
[HttpGet]
1718
[Route("~/api/messages")]
1819
[EnableCors("AllowAll")]
@@ -24,12 +25,12 @@ public JsonResult Get()
2425
.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier)
2526
?.Value;
2627

27-
return Json(new
28+
return new JsonResult(new
2829
{
2930
messages = new dynamic[]
3031
{
31-
new { Date = DateTime.Now, Text = "I am a Robot." },
32-
new { Date = DateTime.Now, Text = "Hello, world!" },
32+
new { Date = DateTime.Now, Text = "I am a Robot." },
33+
new { Date = DateTime.Now, Text = "Hello, world!" },
3334
},
3435
});
3536
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Collections.Generic;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
#pragma warning disable SA1300 // Element should begin with upper-case letter
5+
namespace okta_aspnetcore_webapi_example.Controllers
6+
#pragma warning restore SA1300 // Element should begin with upper-case letter
7+
{
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class ValuesController : ControllerBase
11+
{
12+
// GET api/values
13+
[HttpGet]
14+
public ActionResult<IEnumerable<string>> Get()
15+
{
16+
return new string[] { "value1", "value2" };
17+
}
18+
19+
// GET api/values/5
20+
[HttpGet("{id}")]
21+
public ActionResult<string> Get(int id)
22+
{
23+
return "value";
24+
}
25+
26+
// POST api/values
27+
[HttpPost]
28+
public void Post([FromBody] string value)
29+
{
30+
}
31+
32+
// PUT api/values/5
33+
[HttpPut("{id}")]
34+
public void Put(int id, [FromBody] string value)
35+
{
36+
}
37+
38+
// DELETE api/values/5
39+
[HttpDelete("{id}")]
40+
public void Delete(int id)
41+
{
42+
}
43+
}
44+
}
+11-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
using Microsoft.AspNetCore;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
27
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
310

411
#pragma warning disable SA1300 // Element should begin with upper-case letter
512
namespace okta_aspnetcore_webapi_example
@@ -9,12 +16,11 @@ public class Program
916
{
1017
public static void Main(string[] args)
1118
{
12-
BuildWebHost(args).Run();
19+
CreateWebHostBuilder(args).Build().Run();
1320
}
1421

15-
public static IWebHost BuildWebHost(string[] args) =>
22+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
1623
WebHost.CreateDefaultBuilder(args)
17-
.UseStartup<Startup>()
18-
.Build();
24+
.UseStartup<Startup>();
1925
}
2026
}

Diff for: resource-server/okta-aspnetcore-webapi-example/Properties/launchSettings.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:8000/",
7-
"sslPort": 0
6+
"applicationUrl": "http://localhost:5000",
7+
"sslPort": 44314
88
}
99
},
10+
"$schema": "http://json.schemastore.org/launchsettings.json",
1011
"profiles": {
1112
"IIS Express": {
1213
"commandName": "IISExpress",
@@ -23,7 +24,7 @@
2324
"environmentVariables": {
2425
"ASPNETCORE_ENVIRONMENT": "Development"
2526
},
26-
"applicationUrl": "http://localhost:62707/"
27+
"applicationUrl": "https://localhost:5001;http://localhost:5000"
2728
}
2829
}
2930
}

Diff for: resource-server/okta-aspnetcore-webapi-example/Startup.cs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
using Microsoft.AspNetCore.Authentication.JwtBearer;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
25
using Microsoft.AspNetCore.Builder;
36
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.HttpsPolicy;
8+
using Microsoft.AspNetCore.Mvc;
49
using Microsoft.Extensions.Configuration;
510
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Logging;
12+
using Microsoft.Extensions.Options;
613
using Okta.AspNetCore;
714

815
#pragma warning disable SA1300 // Element should begin with upper-case letter
@@ -38,10 +45,9 @@ public void ConfigureServices(IServiceCollection services)
3845
})
3946
.AddOktaWebApi(new OktaWebApiOptions()
4047
{
41-
OktaDomain = Configuration["Okta:OktaDomain"],
48+
OktaDomain = Configuration["Okta:OktaDomain"],
4249
});
43-
44-
services.AddMvc();
50+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
4551
}
4652

4753
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -51,9 +57,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
5157
{
5258
app.UseDeveloperExceptionPage();
5359
}
60+
else
61+
{
62+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
63+
app.UseHsts();
64+
}
5465

55-
app.UseDefaultFiles();
56-
app.UseStaticFiles();
66+
app.UseHttpsRedirection();
5767
app.UseAuthentication();
5868
app.UseMvc();
5969
}

Diff for: resource-server/okta-aspnetcore-webapi-example/appsettings.Development.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
{
1+
{
22
"Logging": {
3-
"IncludeScopes": false,
43
"LogLevel": {
54
"Default": "Debug",
65
"System": "Information",

Diff for: resource-server/okta-aspnetcore-webapi-example/appsettings.json

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
{
1+
{
22
"Logging": {
3-
"IncludeScopes": false,
4-
"Debug": {
5-
"LogLevel": {
6-
"Default": "Warning"
7-
}
8-
},
9-
"Console": {
10-
"LogLevel": {
11-
"Default": "Warning"
12-
}
3+
"LogLevel": {
4+
"Default": "Warning"
135
}
146
},
7+
"AllowedHosts": "*",
158
"Okta": {
169
"OktaDomain": "https://{yourOktaDomain}"
1710
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
6+
<RootNamespace>okta_aspnetcore_webapi_example</RootNamespace>
57
</PropertyGroup>
6-
7-
<ItemGroup>
8-
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
9-
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
10-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.2" />
11-
<PackageReference Include="Okta.AspNetCore" Version="1.1.4" />
12-
<PackageReference Include="StyleCop.Analyzers" Version="1.1.1-rc.114">
13-
<PrivateAssets>all</PrivateAssets>
14-
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
15-
</PackageReference>
16-
<AdditionalFiles Include="..\stylecop.json" />
17-
</ItemGroup>
18-
198
<PropertyGroup>
209
<CodeAnalysisRuleSet>..\OktaMvcSamples.ruleset</CodeAnalysisRuleSet>
2110
</PropertyGroup>
2211
<ItemGroup>
23-
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
24-
</ItemGroup>
25-
26-
<ItemGroup>
27-
<Folder Include="wwwroot\" />
12+
<PackageReference Include="Microsoft.AspNetCore.App" />
13+
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
14+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
15+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
16+
<PackageReference Include="Okta.AspNetCore" Version="1.1.5" />
17+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
18+
<PrivateAssets>all</PrivateAssets>
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
</PackageReference>
21+
<AdditionalFiles Include="..\stylecop.json" />
2822
</ItemGroup>
2923

3024
</Project>

0 commit comments

Comments
 (0)