2013-10-18 12 views

Trả lời

12

Trong UserManager contructor:

UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false }; 
+0

Tôi không thấy nơi để đặt này. Bạn có thể vui lòng xây dựng? –

+2

Xem câu trả lời của tôi và Kevin Radcliffe để biết các ví dụ hoàn chỉnh. – angularsen

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