Skip to content

Commit

Permalink
Convert to use a Hybrid flow
Browse files Browse the repository at this point in the history
  • Loading branch information
elanderson authored Jun 20, 2017
1 parent 44f8c4b commit ce75a43
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
11 changes: 3 additions & 8 deletions ClientApp/Controllers/IdentityController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Net.Http;
using System.Threading.Tasks;
using IdentityModel.Client;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -11,15 +11,10 @@ public class IdentityController : Controller
[Authorize]
public async Task<IActionResult> Index()
{
var discovery = await DiscoveryClient.GetAsync("http://localhost:5000");

var tokenClient = new TokenClient(discovery.TokenEndpoint, "clientApp", "secret");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("apiApp");

ViewData["tokenResult"] = tokenResponse.IsError ? tokenResponse.Error : tokenResponse.Json.ToString();
var accessToken = await HttpContext.Authentication.GetTokenAsync("access_token");

var client = new HttpClient();
client.SetBearerToken(tokenResponse.AccessToken);
client.SetBearerToken(accessToken);

var apiResponse = await client.GetAsync("http://localhost:5001/api/identity");
ViewData["apiResult"] = apiResponse.IsSuccessStatusCode ? await apiResponse.Content.ReadAsStringAsync() : apiResponse.StatusCode.ToString();
Expand Down
6 changes: 6 additions & 0 deletions ClientApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
RequireHttpsMetadata = false,

ClientId = "mvc",
ClientSecret = "secret",

ResponseType = "code id_token",
Scope = { "apiApp", "offline_access" },

GetClaimsFromUserInfoEndpoint = true,
SaveTokens = true
});

Expand Down
11 changes: 8 additions & 3 deletions ClientApp/Views/Identity/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
@{
@using Microsoft.AspNetCore.Authentication
@{
ViewData["Title"] = "Identity";
}

@ViewData["tokenResult"]
<dt>access token</dt>
<dd>@await ViewContext.HttpContext.Authentication.GetTokenAsync("access_token")</dd>

<dt>refresh token</dt>
<dd>@await ViewContext.HttpContext.Authentication.GetTokenAsync("refresh_token")</dd>

@ViewData["apiResult"]

<h3>User claims</h3>
Expand All @@ -12,7 +18,6 @@
{
<dt>@claim.Type</dt>
<dd>@claim.Value</dd>

}
</dl>

Expand Down
13 changes: 10 additions & 3 deletions IdentityApp/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,23 @@ public static IEnumerable<Client> GetClients()
{
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.Implicit,
AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

ClientSecrets =
{
new Secret("secret".Sha256())
},

RedirectUris = { "http://localhost:5002/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile
}
IdentityServerConstants.StandardScopes.Profile,
"apiApp"
},
AllowOfflineAccess = true
}
};
}
Expand Down

0 comments on commit ce75a43

Please sign in to comment.