Quando si registra un account tramite l'API Web con un messaggio di posta elettronica come "[email protected]", Fiddler restituisce l'errore di follow. Nota che l'e-mail è usata anche per il nome utente, quindi entrambi i campi sono gli stessi. Ma funziona quando si registra su MVC stesso.ASP.NET MVC Identity Email/Username con caratteri speciali
ExceptionMessage = Creazione utente non riuscita - Eccezione identità. Gli errori erano:
Il nome utente [email protected] non è valido, può contenere solo lettere o cifre.
oggetto utente
var newUser = new ApplicationUser {
UserName = user.Email,
Email = user.Email
};
IdentifyConfig.cs
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) {
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager) {
RequireUniqueEmail = true,
AllowOnlyAlphanumericUserNames = false
};
ho cercato commentando AllowOnlyAlphanumericUserNames ma non ha funzionato. Impostandolo correttamente su false, dovrebbero essere consentiti caratteri speciali, nel mio caso un trattino (-).
API controller
// POST: api/auth/register
[ActionName("Register")]
public async Task<HttpResponseMessage> PostRegister(Auth user) {
//dash issue is here.
var userContext = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(userContext);
var userManager = new UserManager<ApplicationUser>(userStore);
var newUser = new ApplicationUser {
UserName = user.Email,
Email = user.Email
};
var result = await userManager.CreateAsync(newUser, user.PasswordHash);
if (result.Succeeded) {
...
Soluzione
non c'è modifiche IdentityConfig.cs. Solo le modifiche riguardano il mio controller API.
// POST: api/auth/register
[ActionName("Register")]
public async Task<HttpResponseMessage> PostRegister(Auth user) {
//Changed to the following line
ApplicationUserManager userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
var newUser = new ApplicationUser {
UserName = user.Email,
Email = user.Email
};
var result = await userManager.CreateAsync(newUser, user.PasswordHash);
Grazie per la soluzione! Ho seguito ciò che hai raccomandato nella tua nota. Ho modificato la mia domanda per riflettere la soluzione di lavoro. :) –