Skip to content
This repository was archived by the owner on Oct 19, 2022. It is now read-only.

Commit 09719f5

Browse files
committed
Add IApplication to service collection (closes #8)
1 parent 8d1bdee commit 09719f5

File tree

5 files changed

+58
-8
lines changed

5 files changed

+58
-8
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// <copyright file="ScopedApplicationAccessor.cs" company="Stormpath, Inc.">
2+
// Copyright (c) 2016 Stormpath, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using Microsoft.AspNetCore.Http;
18+
using Stormpath.Configuration.Abstractions.Immutable;
19+
using Stormpath.Owin.Abstractions;
20+
using Stormpath.SDK.Application;
21+
using Stormpath.SDK.Client;
22+
using Stormpath.SDK.Sync;
23+
24+
namespace Stormpath.AspNetCore
25+
{
26+
internal sealed class ScopedApplicationAccessor
27+
{
28+
private readonly IHttpContextAccessor httpContextAccessor;
29+
30+
public ScopedApplicationAccessor(IHttpContextAccessor httpContextAccessor)
31+
{
32+
this.httpContextAccessor = httpContextAccessor;
33+
}
34+
35+
public IApplication GetItem()
36+
{
37+
var context = this.httpContextAccessor.HttpContext;
38+
var client = context.Items[OwinKeys.StormpathClient] as IClient;
39+
var configuration = context.Items[OwinKeys.StormpathConfiguration] as StormpathConfiguration;
40+
41+
return client.GetApplication(configuration.Application.Href);
42+
}
43+
}
44+
}

src/Stormpath.AspNetCore/ScopedClientAccessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="StormpathContextClientAccessor.cs" company="Stormpath, Inc.">
1+
// <copyright file="ScopedClientAccessor.cs" company="Stormpath, Inc.">
22
// Copyright (c) 2016 Stormpath, Inc.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");

src/Stormpath.AspNetCore/StormpathMiddlewareExtensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using Stormpath.Owin.Abstractions;
2626
using Stormpath.Owin.Middleware;
2727
using Stormpath.Owin.Views.Precompiled;
28+
using Stormpath.SDK.Application;
2829

2930
namespace Stormpath.AspNetCore
3031
{
@@ -51,15 +52,17 @@ public static IServiceCollection AddStormpath(this IServiceCollection services,
5152
services.AddSingleton(new UserConfigurationContainer(configuration));
5253

5354
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
55+
5456
services.AddScoped<ScopedClientAccessor>();
57+
services.AddScoped<ScopedApplicationAccessor>();
5558
services.AddScoped<ScopedLazyUserAccessor>();
56-
57-
services.AddSingleton<RazorViewRenderer>();
58-
5959
services.AddScoped(provider => provider.GetRequiredService<ScopedClientAccessor>().GetItem());
60+
services.AddScoped(provider => provider.GetRequiredService<ScopedApplicationAccessor>().GetItem());
6061
services.AddScoped(provider => provider.GetRequiredService<ScopedLazyUserAccessor>().GetItem());
6162
services.AddScoped(provider => provider.GetRequiredService<ScopedLazyUserAccessor>().GetItem().Value);
6263

64+
services.AddSingleton<RazorViewRenderer>();
65+
6366
services.AddAuthentication();
6467
services.AddAuthorization(options =>
6568
{

src/Stormpath.AspNetCore/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@
3939
"tooling": {
4040
"defaultNamespace": "Stormpath.AspNetCore"
4141
},
42-
"version": "0.3.0"
42+
"version": "0.3.1"
4343
}

test/Stormpath.AspNetCore.TckHarness/Controllers/OpenController.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System;
1818
using System.Threading.Tasks;
1919
using Microsoft.AspNetCore.Mvc;
20+
using Stormpath.SDK.Application;
2021

2122
namespace Stormpath.AspNetCore.TestHarness.Controllers
2223
{
@@ -25,18 +26,20 @@ public class OpenController : Controller
2526
{
2627
private readonly SDK.Client.IClient stormpathClient;
2728
private readonly Lazy<SDK.Account.IAccount> stormpathAccountSafe;
29+
private readonly IApplication stormpathApplication;
2830

29-
public OpenController(SDK.Client.IClient client, Lazy<SDK.Account.IAccount> account)
31+
public OpenController(SDK.Client.IClient client, IApplication application, Lazy<SDK.Account.IAccount> account)
3032
{
3133
this.stormpathClient = client;
34+
this.stormpathApplication = application;
3235
this.stormpathAccountSafe = account;
3336
}
3437

3538
public async Task<string> Get()
3639
{
37-
var tenant = await stormpathClient.GetCurrentTenantAsync();
40+
await stormpathClient.GetCurrentTenantAsync();
3841

39-
return "Hello world";
42+
return $"Hello world from {stormpathApplication.Name}";
4043
}
4144
}
4245
}

0 commit comments

Comments
 (0)