2016-05-16 28 views
6

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); 

答えて

10

あなたはApplicationUserManagerCreate方法でAllowOnlyAlphanumericUserNames = falseを設定しています。 ApiControllerPostRegisterアクション内には、ApplicationUserManagerというインスタンスが新しく追加されています。 AllowOnlyAlphanumericUserNamesはデフォルトでtrueです。

あなたはApplicationUserMangerのインスタンスを新たにするときの方法AllowOnlyAlphanumericUserNamesがfalseに設定されているApplicationUserManager

public ApplicationUserManager(IUserStore<ApplicationUser> store): base(store) 
{ 
     UserValidator = new UserValidator<ApplicationUser>(this) 
     { 
      AllowOnlyAlphanumericUserNames = false, 
      RequireUniqueEmail = true 
     }; 
} 

のコンストラクタを変更することができます。

注: その優れたデフォルトのMVC5テンプレートのAccountControllerで行わまたは依存性の注入を使用すると同じOwinContextからApplicationUserManagerを取得します。

+0

あなたのソリューションをありがとう!私はあなたのメモに推薦したものに従った。私は実際の解決策を反映するために私の質問を編集しました。 :) –

3

あなたの設定がすべてApplicationUserManagerになっています。これはWeb APIのアクションでは使用していません。代わりに、普通の古いバージョンのUserManagerを新しく作成しています。これは、すべての検証などにデフォルトを使用します。

関連する問題