Two factor auth fully working in the sample project.

This commit is contained in:
Judah Gabriel Himango
2018-10-08 12:36:38 -05:00
parent bc757fd13f
commit 6bccf89fd4
4 changed files with 33 additions and 26 deletions
+1
View File
@@ -105,6 +105,7 @@ namespace Raven.Identity
this.Claims = new List<IdentityUserClaim>(); this.Claims = new List<IdentityUserClaim>();
this.Roles = new List<string>(); this.Roles = new List<string>();
this.Logins = new List<UserLoginInfo>(); this.Logins = new List<UserLoginInfo>();
this.TwoFactorRecoveryCodes = new List<string>();
} }
/// <summary> /// <summary>
+2 -2
View File
@@ -11,7 +11,7 @@ public void ConfigureServices(IServiceCollection services)
{ {
// Add RavenDB and identity. // Add RavenDB and identity.
services services
.AddRavenDbAsyncSession(docStore) // Create a RavenDB IAsyncDocumentSession for each request. docStore is your IDocumentStore instance. .AddRavenDbAsyncSession(docStore) // Create a RavenDB IAsyncDocumentSession for each request. docStore is your IDocumentStore instance. You're responsible for calling .SaveChanges after each request.
.AddRavenDbIdentity<AppUser>(); // Use Raven for users and roles. AppUser is your class, a simple DTO to hold user data. See https://github.com/JudahGabriel/RavenDB.Identity/blob/master/Sample/Models/AppUser.cs .AddRavenDbIdentity<AppUser>(); // Use Raven for users and roles. AppUser is your class, a simple DTO to hold user data. See https://github.com/JudahGabriel/RavenDB.Identity/blob/master/Sample/Models/AppUser.cs
... ...
@@ -22,6 +22,6 @@ public void ConfigureServices(IServiceCollection services)
3. You're done! 3. You're done!
Need help? See the [sample app](https://github.com/JudahGabriel/RavenDB.Identity/tree/master/Sample). Need help? See the [sample app](https://github.com/JudahGabriel/RavenDB.Identity/tree/master/Sample). It supports regular sign-ins as well as 2-factor authentication.
Not using .NET Core? See our [sister project](https://github.com/JudahGabriel/RavenDB.AspNet.Identity) for a RavenDB Identity Provider for MVC 5+ and WebAPI 2+ on the full .NET Framework. Not using .NET Core? See our [sister project](https://github.com/JudahGabriel/RavenDB.AspNet.Identity) for a RavenDB Identity Provider for MVC 5+ and WebAPI 2+ on the full .NET Framework.
+9 -6
View File
@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Raven.Client.Documents.Session;
using Sample.Models; using Sample.Models;
using Sample.Models.ManageViewModels; using Sample.Models.ManageViewModels;
using Sample.Services; using Sample.Services;
@@ -18,7 +19,7 @@ namespace Sample.Controllers
{ {
[Authorize] [Authorize]
[Route("[controller]/[action]")] [Route("[controller]/[action]")]
public class ManageController : Controller public class ManageController : RavenController
{ {
private readonly UserManager<AppUser> _userManager; private readonly UserManager<AppUser> _userManager;
private readonly SignInManager<AppUser> _signInManager; private readonly SignInManager<AppUser> _signInManager;
@@ -29,11 +30,13 @@ namespace Sample.Controllers
private const string AuthenicatorUriFormat = "otpauth://totp/{0}:{1}?secret={2}&issuer={0}&digits=6"; private const string AuthenicatorUriFormat = "otpauth://totp/{0}:{1}?secret={2}&issuer={0}&digits=6";
public ManageController( public ManageController(
UserManager<AppUser> userManager, IAsyncDocumentSession dbSession,
SignInManager<AppUser> signInManager, UserManager<AppUser> userManager,
IEmailSender emailSender, SignInManager<AppUser> signInManager,
ILogger<ManageController> logger, IEmailSender emailSender,
UrlEncoder urlEncoder) ILogger<ManageController> logger,
UrlEncoder urlEncoder)
: base(dbSession)
{ {
_userManager = userManager; _userManager = userManager;
_signInManager = signInManager; _signInManager = signInManager;
+21 -18
View File
@@ -29,29 +29,14 @@ namespace Sample
{ {
// Connect to a Raven server. We're using the public test playground at http://live-test.ravendb.net // Connect to a Raven server. We're using the public test playground at http://live-test.ravendb.net
// NOTE: Getting a DatabaseDoesNotExistException? Go to http://live-test.ravendb.net and create a database with the name 'Raven.Identity.Sample' var databaseName = "Raven.Identity.Sample";
var docStore = new DocumentStore var docStore = new DocumentStore
{ {
Urls = new string[] { "http://live-test.ravendb.net" }, Urls = new string[] { "http://live-test.ravendb.net" },
Database = "Raven.Identity.Sample" Database = databaseName
}; };
docStore.Initialize(); docStore.Initialize();
CreateDatabaseIfNotExists(docStore, databaseName);
// Create the database if it doesn't exist yet.
try
{
using (var dbSession = docStore.OpenSession())
{
dbSession.Query<AppUser>().Take(0).ToList();
}
}
catch (Raven.Client.Exceptions.Database.DatabaseDoesNotExistException)
{
docStore.Maintenance.Server.Send(new Raven.Client.ServerWide.Operations.CreateDatabaseOperation(new Raven.Client.ServerWide.DatabaseRecord
{
DatabaseName = "Raven.Identity.Sample"
}));
}
// Add RavenDB and identity. // Add RavenDB and identity.
services services
@@ -67,6 +52,24 @@ namespace Sample
services.AddMvc(); services.AddMvc();
} }
private void CreateDatabaseIfNotExists(IDocumentStore docStore, string databaseName)
{
try
{
using (var dbSession = docStore.OpenSession())
{
dbSession.Query<AppUser>().Take(0).ToList();
}
}
catch (Raven.Client.Exceptions.Database.DatabaseDoesNotExistException)
{
docStore.Maintenance.Server.Send(new Raven.Client.ServerWide.Operations.CreateDatabaseOperation(new Raven.Client.ServerWide.DatabaseRecord
{
DatabaseName = databaseName
}));
}
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ {