Web APIで「[email protected]」のような電子メールでアカウントを登録すると、Fiddlerは次のエラーを返します。 ユーザー名にも電子メールが使用されるので、両方のフィールドが同じであることに注意してください。しかし、それはMVC自体に登録するときに機能します。ASP.NET MVC ID電子メール/特殊文字を含むユーザー名
ExceptionMessage =ユーザーの作成に失敗しました - IDの例外。エラーは次のとおりです。
ユーザー名[email protected]は無効です。文字または数字のみを使用できます。
ユーザーオブジェクト
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
};
私はAllowOnlyAlphanumericUserNamesをコメントアウトしようとしたが、それはうまくいきませんでした。 falseに設定することで、特殊文字を許可する必要があります。私の場合はハイフン( - )を使用します。
APIコントローラ
// 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) {
...
ソリューション
IdentityConfig.csに変更はありません。 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);
あなたのソリューションをありがとう!私はあなたのメモに推薦したものに従った。私は実際の解決策を反映するために私の質問を編集しました。 :) –