Làm cách nào để đặt cờ AllowOnlyAlphanumericUserNames trên Microsoft.AspNet.Identity.UserManager để UserValidator cho phép UserName không phải là chữ và số?AllowOnlyAlphanumericUserNames - cách đặt nó? (RC to RTM breaking change) ASP.NET Identity
16
A
Trả lời
12
Trong UserManager contructor:
UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
6
câu trả lời của John là đúng, tôi đã sử dụng câu trả lời của mình để cho phép email như tên người dùng (không hoạt động theo mặc định)
Hãy upvote/chấp nhận câu trả lời của John.
Dưới đây là một số mã mà tôi sử dụng một UserManager tùy chỉnh" để có được những điều làm việc
(Bằng cách này, có ít lặp lại ở những nơi khác quá)
public class MyUserManager : UserManager<ApplicationUser>
{
public MyUserManager(DbContext db)
: base(new UserStore<ApplicationUser>(db))
{
this.UserValidator = UserValidator = new UserValidator<ApplicationUser>(this)
{ AllowOnlyAlphanumericUserNames = false };
}
}
Và đây là những gì mã AccountController Constructor trông giống như bây giờ:
[Authorize]
public class AccountController : Controller
{
public AccountController()
: this(new MyUserManager(new AppContext()))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
}
public UserManager<ApplicationUser> UserManager { get; private set; }
7
Tuy nhiên, một cách khác để làm việc đó:
[Authorize]
public class AccountController : Controller
{
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
// Start of new code
UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager)
{
AllowOnlyAlphanumericUserNames = false,
};
// End of new code
}
public UserManager<ApplicationUser> UserManager { get; private set; }
}
0
Bạn có thể viết UserValidator của riêng bạn như this. Và sau đó sử dụng nó:
var userManager = new UserManager<ApplicationUser>(new CustomUserStore());
userManager.UserValidator = new CustomUserValidator<ApplicationUser>(userManager);
1
cách khác làm
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
2
Tính đến ASP.NET nhận dạng 3.0 (hiện đang trong RC), điều này bây giờ được cấu hình như một tùy chọn trên người dùng.
public void ConfigureServices(IServiceCollection services)
{
// (Rest of code removed)
// Note the + added to the string of allowed user name characters
services.AddIdentity<ApplicationUser, IdentityRole>(o => o.User.AllowedUserNameCharacters = @"[email protected]+")
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
}
Cùng mã như một Gist: https://gist.github.com/pollax/4449ce7cf47bde6b3a95
Các vấn đề liên quan
- 1. Không thể cài đặt ASP.NET MVC3 RTM?
- 2. Cách nâng cấp Visual Studio 2015 RC lên RTM
- 3. Cài đặt Windows Phone 7.1 SDK trên Windows 8.1 RTM với Visual Studio 2013 RC
- 4. Cách cấu hình ASP.NET Identity ApplicationUserManager với StructureMap
- 5. ASP.net MVC RTM Kiểm tra quy ước đặt tên
- 6. ASP.NET Core Change Build directory
- 7. ASP.NET Identity không được đặt trong yêu cầu đăng nhập
- 8. Làm thế nào để đặt ASP.net Identity Vai trò vào Identityserver4 Identity token
- 9. ASP.NET Identity & ASP.NET Membership Provider "Mashup"
- 10. Tương lai RTM MVC3 - Cài đặt Microsoft.Web.Mvc
- 11. Sử dụng Reflection to Change Properties trong Swift
- 12. ASP.NET Identity + Token Bearer + Multi-Tenant
- 13. ASP.NET Core Identity 3 Cookie timeout
- 14. Người dùng cập nhật Asp.net Identity 2.0
- 15. ASP.NET Identity WebAPI mã thông báo đặt lại mật khẩu không hợp lệ
- 16. ASP.NET Identity với nhiều ứng dụng
- 17. đặt trình nghe "On Change" trên jFormattedTextField
- 18. RTM trong MVC 3 RTM standfor
- 19. ASP.NET 5 Identity - tùy chỉnh SignInManager
- 20. ASP.net Identity SecurityStampValidator OnValidateIdentity regenerateIdentity tham số
- 21. ASP.NET Identity không có Entity Framework
- 22. thử nghiệm tích hợp ASP.NET 5 Identity
- 23. Breaking the Build, Tại sao nó là một điều xấu?
- 24. Làm cách nào để thực hiện đặt lại mật khẩu với ASP.NET Identity cho ASP.NET MVC 5.0?
- 25. ASP Identity 2.0: Regenerate Identity
- 26. Breaking ReadFile() chặn - Đặt tên Pipe (Windows API)
- 27. LINQ to SQL - Cách tìm giá trị của cột IDENTITY sau khi InsertOnSubmit()
- 28. Breaking the constructor
- 29. Asp.net Mvc 2.0 RC Thiết lập Chủ đề CurrentCulture/Ui
- 30. ASP.NET Core Middleware Passing Parameters to Controllers
Tôi không thấy nơi để đặt này. Bạn có thể vui lòng xây dựng? –
Xem câu trả lời của tôi và Kevin Radcliffe để biết các ví dụ hoàn chỉnh. – angularsen