How to set up TwoFactor and Lockout settings with ASP.NET Boilerplate templates?
I've been using ASP.NET Boilerplate as a template for my application as a starting point. I was in the process of modifying the login process to implement 2FA. However, I'm not sure where to put those configuration settings. In a previous application using .NET MVC with .NET Identity, I was able to do this in a class we had as ApplicationUserManager
that inherited UserManager
. This is how I had things configured:
public class ApplicationUserManager : UserManager<User>
{
public ApplicationUserManager(IUserStore<User> store) : base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<User>(context.Get<Db>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<User>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromDays(365*500);
manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<User>()
{
Subject = "Security Code",
BodyFormat = "Your security code is: {0}"
});
manager.EmailService = new EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
In ASP.NET Boilerplate, there are a lot of powerful tools and many interfaces that can be implemented to assist in the login process. I've seen a GitHub post where disable lockout feature was discussed, and it looks like I should be able to add to my SettingsManager
database options for MaxFailedAccessAttempts
and DefaultLockoutTimeSpan
, but I'm not sure that just by adding them to the database that they will be picked up auto-magically, especially if I do not name them correctly. There is already a class of constant values for EmailSettings
that I was able to use here:
public void Create()
{
// Emailing
AddSettingIfNotExists(EmailSettingNames.DefaultFromAddress, "developer@developer.com");
AddSettingIfNotExists(EmailSettingNames.DefaultFromDisplayName, "do not reply");
AddSettingIfNotExists(EmailSettingNames.Smtp.Host, "smtp.gmail.com");
AddSettingIfNotExists(EmailSettingNames.Smtp.Port, "587");
AddSettingIfNotExists(EmailSettingNames.Smtp.UserName, "developer@developer.com");
AddSettingIfNotExists(EmailSettingNames.Smtp.Password, "SomePassword");
AddSettingIfNotExists(EmailSettingNames.Smtp.EnableSsl, "true");
AddSettingIfNotExists(EmailSettingNames.Smtp.UseDefaultCredentials, "false");
// Languages
AddSettingIfNotExists(LocalizationSettingNames.DefaultLanguage, "en");
}
There is also a class in the Core
project — AppSettingNames
, which contains a value for UiTheme
. I suppose I can add the constant values there. However, if I do this, do I need to name these settings identically to what the UserManager
attributes are looking for?
c# asp.net-core settings aspnetboilerplate asp.net-core-identity
add a comment |
I've been using ASP.NET Boilerplate as a template for my application as a starting point. I was in the process of modifying the login process to implement 2FA. However, I'm not sure where to put those configuration settings. In a previous application using .NET MVC with .NET Identity, I was able to do this in a class we had as ApplicationUserManager
that inherited UserManager
. This is how I had things configured:
public class ApplicationUserManager : UserManager<User>
{
public ApplicationUserManager(IUserStore<User> store) : base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<User>(context.Get<Db>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<User>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromDays(365*500);
manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<User>()
{
Subject = "Security Code",
BodyFormat = "Your security code is: {0}"
});
manager.EmailService = new EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
In ASP.NET Boilerplate, there are a lot of powerful tools and many interfaces that can be implemented to assist in the login process. I've seen a GitHub post where disable lockout feature was discussed, and it looks like I should be able to add to my SettingsManager
database options for MaxFailedAccessAttempts
and DefaultLockoutTimeSpan
, but I'm not sure that just by adding them to the database that they will be picked up auto-magically, especially if I do not name them correctly. There is already a class of constant values for EmailSettings
that I was able to use here:
public void Create()
{
// Emailing
AddSettingIfNotExists(EmailSettingNames.DefaultFromAddress, "developer@developer.com");
AddSettingIfNotExists(EmailSettingNames.DefaultFromDisplayName, "do not reply");
AddSettingIfNotExists(EmailSettingNames.Smtp.Host, "smtp.gmail.com");
AddSettingIfNotExists(EmailSettingNames.Smtp.Port, "587");
AddSettingIfNotExists(EmailSettingNames.Smtp.UserName, "developer@developer.com");
AddSettingIfNotExists(EmailSettingNames.Smtp.Password, "SomePassword");
AddSettingIfNotExists(EmailSettingNames.Smtp.EnableSsl, "true");
AddSettingIfNotExists(EmailSettingNames.Smtp.UseDefaultCredentials, "false");
// Languages
AddSettingIfNotExists(LocalizationSettingNames.DefaultLanguage, "en");
}
There is also a class in the Core
project — AppSettingNames
, which contains a value for UiTheme
. I suppose I can add the constant values there. However, if I do this, do I need to name these settings identically to what the UserManager
attributes are looking for?
c# asp.net-core settings aspnetboilerplate asp.net-core-identity
add a comment |
I've been using ASP.NET Boilerplate as a template for my application as a starting point. I was in the process of modifying the login process to implement 2FA. However, I'm not sure where to put those configuration settings. In a previous application using .NET MVC with .NET Identity, I was able to do this in a class we had as ApplicationUserManager
that inherited UserManager
. This is how I had things configured:
public class ApplicationUserManager : UserManager<User>
{
public ApplicationUserManager(IUserStore<User> store) : base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<User>(context.Get<Db>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<User>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromDays(365*500);
manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<User>()
{
Subject = "Security Code",
BodyFormat = "Your security code is: {0}"
});
manager.EmailService = new EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
In ASP.NET Boilerplate, there are a lot of powerful tools and many interfaces that can be implemented to assist in the login process. I've seen a GitHub post where disable lockout feature was discussed, and it looks like I should be able to add to my SettingsManager
database options for MaxFailedAccessAttempts
and DefaultLockoutTimeSpan
, but I'm not sure that just by adding them to the database that they will be picked up auto-magically, especially if I do not name them correctly. There is already a class of constant values for EmailSettings
that I was able to use here:
public void Create()
{
// Emailing
AddSettingIfNotExists(EmailSettingNames.DefaultFromAddress, "developer@developer.com");
AddSettingIfNotExists(EmailSettingNames.DefaultFromDisplayName, "do not reply");
AddSettingIfNotExists(EmailSettingNames.Smtp.Host, "smtp.gmail.com");
AddSettingIfNotExists(EmailSettingNames.Smtp.Port, "587");
AddSettingIfNotExists(EmailSettingNames.Smtp.UserName, "developer@developer.com");
AddSettingIfNotExists(EmailSettingNames.Smtp.Password, "SomePassword");
AddSettingIfNotExists(EmailSettingNames.Smtp.EnableSsl, "true");
AddSettingIfNotExists(EmailSettingNames.Smtp.UseDefaultCredentials, "false");
// Languages
AddSettingIfNotExists(LocalizationSettingNames.DefaultLanguage, "en");
}
There is also a class in the Core
project — AppSettingNames
, which contains a value for UiTheme
. I suppose I can add the constant values there. However, if I do this, do I need to name these settings identically to what the UserManager
attributes are looking for?
c# asp.net-core settings aspnetboilerplate asp.net-core-identity
I've been using ASP.NET Boilerplate as a template for my application as a starting point. I was in the process of modifying the login process to implement 2FA. However, I'm not sure where to put those configuration settings. In a previous application using .NET MVC with .NET Identity, I was able to do this in a class we had as ApplicationUserManager
that inherited UserManager
. This is how I had things configured:
public class ApplicationUserManager : UserManager<User>
{
public ApplicationUserManager(IUserStore<User> store) : base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<User>(context.Get<Db>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<User>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromDays(365*500);
manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<User>()
{
Subject = "Security Code",
BodyFormat = "Your security code is: {0}"
});
manager.EmailService = new EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
In ASP.NET Boilerplate, there are a lot of powerful tools and many interfaces that can be implemented to assist in the login process. I've seen a GitHub post where disable lockout feature was discussed, and it looks like I should be able to add to my SettingsManager
database options for MaxFailedAccessAttempts
and DefaultLockoutTimeSpan
, but I'm not sure that just by adding them to the database that they will be picked up auto-magically, especially if I do not name them correctly. There is already a class of constant values for EmailSettings
that I was able to use here:
public void Create()
{
// Emailing
AddSettingIfNotExists(EmailSettingNames.DefaultFromAddress, "developer@developer.com");
AddSettingIfNotExists(EmailSettingNames.DefaultFromDisplayName, "do not reply");
AddSettingIfNotExists(EmailSettingNames.Smtp.Host, "smtp.gmail.com");
AddSettingIfNotExists(EmailSettingNames.Smtp.Port, "587");
AddSettingIfNotExists(EmailSettingNames.Smtp.UserName, "developer@developer.com");
AddSettingIfNotExists(EmailSettingNames.Smtp.Password, "SomePassword");
AddSettingIfNotExists(EmailSettingNames.Smtp.EnableSsl, "true");
AddSettingIfNotExists(EmailSettingNames.Smtp.UseDefaultCredentials, "false");
// Languages
AddSettingIfNotExists(LocalizationSettingNames.DefaultLanguage, "en");
}
There is also a class in the Core
project — AppSettingNames
, which contains a value for UiTheme
. I suppose I can add the constant values there. However, if I do this, do I need to name these settings identically to what the UserManager
attributes are looking for?
c# asp.net-core settings aspnetboilerplate asp.net-core-identity
c# asp.net-core settings aspnetboilerplate asp.net-core-identity
edited Nov 21 at 14:07
aaron
8,46931130
8,46931130
asked Nov 20 at 17:48
TroyB
6512
6512
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The relevant constants are in AbpZeroSettingNames
.
// DefaultLockoutTimeSpan
AddSettingIfNotExists(
AbpZeroSettingNames.UserManagement.UserLockOut.DefaultAccountLockoutSeconds,
int.MaxValue.ToString() // 2,147,483,647 seconds, just over 68 years
);
// MaxFailedAccessAttempts
AddSettingIfNotExists(
AbpZeroSettingNames.UserManagement.UserLockOut.MaxFailedAccessAttemptsBeforeLockout,
"5"
);
Unfortunately, int.MaxValue
in seconds only allows the application to lock out the user for just over 68 years at a time, shy of the 500 years that you used to configure.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398702%2fhow-to-set-up-twofactor-and-lockout-settings-with-asp-net-boilerplate-templates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The relevant constants are in AbpZeroSettingNames
.
// DefaultLockoutTimeSpan
AddSettingIfNotExists(
AbpZeroSettingNames.UserManagement.UserLockOut.DefaultAccountLockoutSeconds,
int.MaxValue.ToString() // 2,147,483,647 seconds, just over 68 years
);
// MaxFailedAccessAttempts
AddSettingIfNotExists(
AbpZeroSettingNames.UserManagement.UserLockOut.MaxFailedAccessAttemptsBeforeLockout,
"5"
);
Unfortunately, int.MaxValue
in seconds only allows the application to lock out the user for just over 68 years at a time, shy of the 500 years that you used to configure.
add a comment |
The relevant constants are in AbpZeroSettingNames
.
// DefaultLockoutTimeSpan
AddSettingIfNotExists(
AbpZeroSettingNames.UserManagement.UserLockOut.DefaultAccountLockoutSeconds,
int.MaxValue.ToString() // 2,147,483,647 seconds, just over 68 years
);
// MaxFailedAccessAttempts
AddSettingIfNotExists(
AbpZeroSettingNames.UserManagement.UserLockOut.MaxFailedAccessAttemptsBeforeLockout,
"5"
);
Unfortunately, int.MaxValue
in seconds only allows the application to lock out the user for just over 68 years at a time, shy of the 500 years that you used to configure.
add a comment |
The relevant constants are in AbpZeroSettingNames
.
// DefaultLockoutTimeSpan
AddSettingIfNotExists(
AbpZeroSettingNames.UserManagement.UserLockOut.DefaultAccountLockoutSeconds,
int.MaxValue.ToString() // 2,147,483,647 seconds, just over 68 years
);
// MaxFailedAccessAttempts
AddSettingIfNotExists(
AbpZeroSettingNames.UserManagement.UserLockOut.MaxFailedAccessAttemptsBeforeLockout,
"5"
);
Unfortunately, int.MaxValue
in seconds only allows the application to lock out the user for just over 68 years at a time, shy of the 500 years that you used to configure.
The relevant constants are in AbpZeroSettingNames
.
// DefaultLockoutTimeSpan
AddSettingIfNotExists(
AbpZeroSettingNames.UserManagement.UserLockOut.DefaultAccountLockoutSeconds,
int.MaxValue.ToString() // 2,147,483,647 seconds, just over 68 years
);
// MaxFailedAccessAttempts
AddSettingIfNotExists(
AbpZeroSettingNames.UserManagement.UserLockOut.MaxFailedAccessAttemptsBeforeLockout,
"5"
);
Unfortunately, int.MaxValue
in seconds only allows the application to lock out the user for just over 68 years at a time, shy of the 500 years that you used to configure.
edited Nov 21 at 13:57
answered Nov 21 at 13:51
aaron
8,46931130
8,46931130
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398702%2fhow-to-set-up-twofactor-and-lockout-settings-with-asp-net-boilerplate-templates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown