Replace AlphaNumericUserName with Regex option
This commit is contained in:
parent
3f4f846cbb
commit
57002ba359
|
|
@ -1,20 +1,18 @@
|
||||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Identity
|
namespace Microsoft.AspNet.Identity
|
||||||
{
|
{
|
||||||
public class UserOptions
|
public class UserOptions
|
||||||
{
|
{
|
||||||
public UserOptions()
|
public UserOptions()
|
||||||
{
|
{
|
||||||
AllowOnlyAlphanumericNames = true;
|
|
||||||
//User.RequireUniqueEmail = true; // TODO: app decision?
|
//User.RequireUniqueEmail = true; // TODO: app decision?
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public string UserNameValidationRegex { get; set; } = "^[a-zA-Z0-9@_\\.]+$";
|
||||||
/// Only allow [A-Za-z0-9@_] in UserNames
|
|
||||||
/// </summary>
|
|
||||||
public bool AllowOnlyAlphanumericNames { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If set, enforces that emails are non empty, valid, and unique
|
/// If set, enforces that emails are non empty, valid, and unique
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
|
||||||
#if ASPNET50
|
#if ASPNET50
|
||||||
using System.Net.Mail;
|
using System.Net.Mail;
|
||||||
#endif
|
#endif
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
|
@ -46,48 +46,6 @@ namespace Microsoft.AspNet.Identity
|
||||||
return errors.Count > 0 ? IdentityResult.Failed(errors.ToArray()) : IdentityResult.Success;
|
return errors.Count > 0 ? IdentityResult.Failed(errors.ToArray()) : IdentityResult.Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Revisit extensibility for Validators
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if the character is a digit between '0' and '9'
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="c"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public virtual bool IsDigit(char c)
|
|
||||||
{
|
|
||||||
return c >= '0' && c <= '9';
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if the character is between 'a' and 'z'
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="c"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public virtual bool IsLower(char c)
|
|
||||||
{
|
|
||||||
return c >= 'a' && c <= 'z';
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if the character is between 'A' and 'Z'
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="c"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public virtual bool IsUpper(char c)
|
|
||||||
{
|
|
||||||
return c >= 'A' && c <= 'Z';
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if the character is upper, lower, a digit, or a common email character [@_.]
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="c"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public virtual bool IsAlphaNumeric(char c)
|
|
||||||
{
|
|
||||||
return IsUpper(c) || IsLower(c) || IsDigit(c) || c == '@' || c == '_' || c == '.';
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task ValidateUserName(UserManager<TUser> manager, TUser user, ICollection<string> errors)
|
private async Task ValidateUserName(UserManager<TUser> manager, TUser user, ICollection<string> errors)
|
||||||
{
|
{
|
||||||
var userName = await manager.GetUserNameAsync(user);
|
var userName = await manager.GetUserNameAsync(user);
|
||||||
|
|
@ -95,9 +53,8 @@ namespace Microsoft.AspNet.Identity
|
||||||
{
|
{
|
||||||
errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.PropertyTooShort, "UserName"));
|
errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.PropertyTooShort, "UserName"));
|
||||||
}
|
}
|
||||||
else if (manager.Options.User.AllowOnlyAlphanumericNames && !userName.All(IsAlphaNumeric))
|
else if (manager.Options.User.UserNameValidationRegex != null && !Regex.IsMatch(userName, manager.Options.User.UserNameValidationRegex))
|
||||||
{
|
{
|
||||||
// If any characters are not letters or digits, its an illegal user name
|
|
||||||
errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.InvalidUserName, userName));
|
errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.InvalidUserName, userName));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@
|
||||||
"System.Security.Cryptography.Hashing.Algorithms": "4.0.0.0",
|
"System.Security.Cryptography.Hashing.Algorithms": "4.0.0.0",
|
||||||
"System.Text.Encoding": "4.0.10.0",
|
"System.Text.Encoding": "4.0.10.0",
|
||||||
"System.Text.Encoding.Extensions": "4.0.10.0",
|
"System.Text.Encoding.Extensions": "4.0.10.0",
|
||||||
|
"System.Text.RegularExpressions": "4.0.0.0",
|
||||||
"System.Threading.Tasks": "4.0.10.0"
|
"System.Threading.Tasks": "4.0.10.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ namespace Microsoft.AspNet.Identity.InMemory.Test
|
||||||
options.Password.RequireLowercase = false;
|
options.Password.RequireLowercase = false;
|
||||||
options.Password.RequireNonLetterOrDigit = false;
|
options.Password.RequireNonLetterOrDigit = false;
|
||||||
options.Password.RequireUppercase = false;
|
options.Password.RequireUppercase = false;
|
||||||
options.User.AllowOnlyAlphanumericNames = false;
|
options.User.UserNameValidationRegex = null;
|
||||||
});
|
});
|
||||||
return services.BuildServiceProvider().GetService<UserManager<IdentityUser>>();
|
return services.BuildServiceProvider().GetService<UserManager<IdentityUser>>();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ namespace Microsoft.AspNet.Identity.SqlServer.InMemory.Test
|
||||||
options.Password.RequireLowercase = false;
|
options.Password.RequireLowercase = false;
|
||||||
options.Password.RequireNonLetterOrDigit = false;
|
options.Password.RequireNonLetterOrDigit = false;
|
||||||
options.Password.RequireUppercase = false;
|
options.Password.RequireUppercase = false;
|
||||||
options.User.AllowOnlyAlphanumericNames = false;
|
options.User.UserNameValidationRegex = null;
|
||||||
});
|
});
|
||||||
return services.BuildServiceProvider().GetService<UserManager<IdentityUser>>();
|
return services.BuildServiceProvider().GetService<UserManager<IdentityUser>>();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -146,7 +146,7 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test
|
||||||
options.Password.RequireNonLetterOrDigit = false;
|
options.Password.RequireNonLetterOrDigit = false;
|
||||||
options.Password.RequireUppercase = false;
|
options.Password.RequireUppercase = false;
|
||||||
options.Password.RequireDigit = false;
|
options.Password.RequireDigit = false;
|
||||||
options.User.AllowOnlyAlphanumericNames = false;
|
options.User.UserNameValidationRegex = null;
|
||||||
});
|
});
|
||||||
services.SetupOptions<DbContextOptions>(options =>
|
services.SetupOptions<DbContextOptions>(options =>
|
||||||
options.UseSqlServer(ConnectionString));
|
options.UseSqlServer(ConnectionString));
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ namespace Microsoft.AspNet.Identity.Test
|
||||||
Assert.True(options.Password.RequireUppercase);
|
Assert.True(options.Password.RequireUppercase);
|
||||||
Assert.Equal(6, options.Password.RequiredLength);
|
Assert.Equal(6, options.Password.RequiredLength);
|
||||||
|
|
||||||
Assert.True(options.User.AllowOnlyAlphanumericNames);
|
Assert.Equal("^[a-zA-Z0-9@_\\.]+$", options.User.UserNameValidationRegex);
|
||||||
Assert.False(options.User.RequireUniqueEmail);
|
Assert.False(options.User.RequireUniqueEmail);
|
||||||
|
|
||||||
Assert.Equal(ClaimTypes.Role, options.ClaimsIdentity.RoleClaimType);
|
Assert.Equal(ClaimTypes.Role, options.ClaimsIdentity.RoleClaimType);
|
||||||
|
|
@ -77,8 +77,7 @@ namespace Microsoft.AspNet.Identity.Test
|
||||||
Assert.Equal(usernameClaimType, options.ClaimsIdentity.UserNameClaimType);
|
Assert.Equal(usernameClaimType, options.ClaimsIdentity.UserNameClaimType);
|
||||||
Assert.Equal(securityStampClaimType, options.ClaimsIdentity.SecurityStampClaimType);
|
Assert.Equal(securityStampClaimType, options.ClaimsIdentity.SecurityStampClaimType);
|
||||||
Assert.True(options.User.RequireUniqueEmail);
|
Assert.True(options.User.RequireUniqueEmail);
|
||||||
Assert.True(options.User.AllowOnlyAlphanumericNames);
|
Assert.Equal("^[a-zA-Z0-9@_\\.]+$", options.User.UserNameValidationRegex);
|
||||||
Assert.True(options.User.AllowOnlyAlphanumericNames);
|
|
||||||
Assert.False(options.Password.RequireDigit);
|
Assert.False(options.Password.RequireDigit);
|
||||||
Assert.False(options.Password.RequireLowercase);
|
Assert.False(options.Password.RequireLowercase);
|
||||||
Assert.False(options.Password.RequireNonLetterOrDigit);
|
Assert.False(options.Password.RequireNonLetterOrDigit);
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ namespace Microsoft.AspNet.Identity.Test
|
||||||
{
|
{
|
||||||
// Setup
|
// Setup
|
||||||
var manager = MockHelpers.TestUserManager(new NoopUserStore());
|
var manager = MockHelpers.TestUserManager(new NoopUserStore());
|
||||||
manager.Options.User.AllowOnlyAlphanumericNames = false;
|
manager.Options.User.UserNameValidationRegex = null;
|
||||||
var validator = new UserValidator<TestUser>();
|
var validator = new UserValidator<TestUser>();
|
||||||
var user = new TestUser {UserName = userName};
|
var user = new TestUser {UserName = userName};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ namespace Microsoft.AspNet.Identity.Test
|
||||||
options.Password.RequireLowercase = false;
|
options.Password.RequireLowercase = false;
|
||||||
options.Password.RequireNonLetterOrDigit = false;
|
options.Password.RequireNonLetterOrDigit = false;
|
||||||
options.Password.RequireUppercase = false;
|
options.Password.RequireUppercase = false;
|
||||||
options.User.AllowOnlyAlphanumericNames = false;
|
options.User.UserNameValidationRegex = null;
|
||||||
});
|
});
|
||||||
return services.BuildServiceProvider().GetService<UserManager<TUser>>();
|
return services.BuildServiceProvider().GetService<UserManager<TUser>>();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -392,7 +392,7 @@ namespace Microsoft.AspNet.Identity.Test
|
||||||
{
|
{
|
||||||
var manager = CreateManager();
|
var manager = CreateManager();
|
||||||
manager.Options.User.UseUserNameAsEmail = true;
|
manager.Options.User.UseUserNameAsEmail = true;
|
||||||
manager.Options.User.AllowOnlyAlphanumericNames = false;
|
manager.Options.User.UserNameValidationRegex = null;
|
||||||
var user = CreateTestUser();
|
var user = CreateTestUser();
|
||||||
var email = user.UserName + "@test.com";
|
var email = user.UserName + "@test.com";
|
||||||
user.UserName = email;
|
user.UserName = email;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue