2016-04-26 6 views
3

を働いていない私が管理するユーザーのためのasp.netのIDを使用し これは、ユーザーAsp.netアイデンティティの電子メールの検証が私のWebアプリケーションで

[AllowAnonymous] 
    public async Task<IHttpActionResult> Register(UserRegisterJson userRegisterJson) 
    { 
     IUserManagement userManagement = new UserManagement(); 
     var user = userManagement.GetUserFromJson(userRegisterJson); 

     var identityResult = await UserManager.CreateAsync(user, userRegisterJson.Password); 

     if (!identityResult.Succeeded) 
     { 
      //return error 
     }else{ 
      return Ok(true); 
     } 


    } 

を登録するための私のregisterメソッドである私がリクエストボディでJSON形式などのユーザ情報を送ります問題は、ユーザーのメールが「example」または「example @ example」と等しい場合です。または "例の@試験ple.com"

は偽

を返すidentityResult.Succeededが、ユーザーの電子メール"は、例えば@例"

に等しいとき真を返しidentityResult.Succeeded。

私の質問は、電子メールは、 "例@例" に等しいなぜ

はtrueを返しidentityResult.Succeededのですか?

+0

どのようにユーザーのメールを検証していますか? – Nkosi

+0

私は自分のプロジェクトに静的メソッドを持っており、identityResult.Succeededがfalseを返す場合はエラーを返しますが、identityResult.Succeededは "example @ example"と等しい場合にtrueを返します。 –

+0

どのようにユーザーを検証していますかユーザーを作成する前に電子メール。クライアント側?サーバ側?あなたのモデル 'UserRegisterJson'に検証属性がありますか?あなたの質問からこれを抽出することはできませんので、あなたを助けることはあまりありません。 – Nkosi

答えて

0

source code of UserValidator for that verison (v2.2.1)を見ると、以下の方法がUserManager.CreateAsyncの側で呼び出されていました。

// make sure email is not empty, valid, and unique 
private async Task ValidateEmailAsync(TUser user, List<string> errors) 
{ 
    var email = await Manager.GetEmailStore().GetEmailAsync(user).WithCurrentCulture(); 
    if (string.IsNullOrWhiteSpace(email)) 
    { 
     errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.PropertyTooShort, "Email")); 
     return; 
    } 
    try 
    { 
     var m = new MailAddress(email); 
    } 
    catch (FormatException) 
    { 
     errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.InvalidEmail, email)); 
     return; 
    } 
    var owner = await Manager.FindByEmailAsync(email).WithCurrentCulture(); 
    if (owner != null && !EqualityComparer<TKey>.Default.Equals(owner.Id, user.Id)) 
    { 
     errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.DuplicateEmail, email)); 
    } 
} 

あなたは提供する電子メールアドレスを使用してMailAddressオブジェクトを作成しようとしている見ることができるように。アドレスが有効な形式でない場合、それは失敗するはずです。

これまでに使用されていたフォーマットがあれば、私が提供したサンプルを検証するための単体テストを作成しました。

[DataDrivenTestMethod] 
[DataRow("example")] 
[DataRow("[email protected]")] 
[DataRow("[email protected] ple.com")] 
[DataRow("[email protected]")] 
public void ValidateEmailAddress(string email) { 
    var m = new System.Net.Mail.MailAddress(email); 
    Assert.IsNotNull(m); 
} 

次のような結果が

Result Message: 
Assert.IsTrue failed. 

DataRow: email: example 
Summary: Exception has been thrown by the target of an invocation. 

DataRow: email: [email protected] ple.com 
Summary: Exception has been thrown by the target of an invocation. 

example[email protected] ple.comを返されたが、そのロジックに従って有効なEメールアドレスとはみなされません。

新しいユーザーを作成する前に、モデルで自分のメール検証を実行することをお勧めします。

関連する問題