Skip to content

Commit 8c715de

Browse files
committed
Added dummy authentication
1 parent 49ec6e4 commit 8c715de

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

LinkDotNet.Blog.Web/Authentication/Auth0/Auth0LoginManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task SignInAsync(string redirectUri)
2525

2626
public async Task SignOutAsync(string redirectUri = "/")
2727
{
28-
await httpContext.SignOutAsync("Auth0", new AuthenticationProperties { RedirectUri = "/" });
28+
await httpContext.SignOutAsync("Auth0", new AuthenticationProperties { RedirectUri = redirectUri });
2929
await httpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
3030
}
3131
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.AspNetCore.Authentication.Cookies;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace LinkDotNet.Blog.Web.Authentication.Dummy
6+
{
7+
public static class DummyExtensions
8+
{
9+
public static void UseDummyAuthentication(this IServiceCollection services)
10+
{
11+
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
12+
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
13+
options =>
14+
{
15+
options.LoginPath = new PathString("/login");
16+
});
17+
18+
services.AddAuthorization();
19+
services.AddHttpContextAccessor();
20+
services.AddScoped<ILoginManager, DummyLoginManager>();
21+
}
22+
}
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Security.Claims;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Authentication;
5+
using Microsoft.AspNetCore.Authentication.Cookies;
6+
using Microsoft.AspNetCore.Http;
7+
8+
namespace LinkDotNet.Blog.Web.Authentication.Dummy
9+
{
10+
public class DummyLoginManager : ILoginManager
11+
{
12+
private readonly HttpContext context;
13+
14+
public DummyLoginManager(IHttpContextAccessor httpContextAccessor)
15+
{
16+
context = httpContextAccessor?.HttpContext ?? throw new NotSupportedException("I need HttpContext. Njom njom njom");
17+
}
18+
19+
public async Task SignOutAsync(string redirectUri = "/")
20+
{
21+
await context.SignOutAsync();
22+
context.Response.Redirect(redirectUri);
23+
}
24+
25+
public async Task SignInAsync(string redirectUri)
26+
{
27+
var claims = new[]
28+
{
29+
new Claim(ClaimTypes.Name, "Dummy user"),
30+
new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()),
31+
};
32+
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
33+
var principal = new ClaimsPrincipal(identity);
34+
35+
await context.SignInAsync(principal, null);
36+
context.Response.Redirect(redirectUri);
37+
}
38+
}
39+
}

LinkDotNet.Blog.Web/Startup.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public void ConfigureServices(IServiceCollection services)
3838

3939
// Here you can setup up your identity provider
4040
services.UseAuth0Authentication(Configuration);
41+
/****************
42+
* Possible authentication mechanism:
43+
* services.UseDummyAuthentication();
44+
*/
4145

4246
services.AddBlazoredToast();
4347
services.AddBlazoredLocalStorage();

Readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ services.UseSqlAsStorageProvider();
7474
It is only one storage provider at a time allowed. Registering multiple will result in an exception.
7575

7676
## Authorization
77-
There is only one mechanism enabled via Auth0. For more information go to: https://auth0.com/docs/applications
77+
There is only one real mechanism enabled via Auth0. For more information go to: https://auth0.com/docs/applications
7878

79-
The main advantage of Auth0 is the easy configurable dashboard on their website.
79+
The main advantage of Auth0 is the easy configurable dashboard on their website.
80+
81+
For testing purposes you can use `services.UseDummyAuthentication();`. This allows every user, who presses Login, to be logged in.
8082

8183
## Search Engine Optimization (SEO)
8284
The blog includes some of the most important tags to get indexed by a crawler. Furthermore some aspects of the Open Graph specification are implemented.

0 commit comments

Comments
 (0)