2016-07-26 19 views
0

當使用包含連字符([email protected])的電子郵件註冊用戶時,用戶將以正確的角色正確登錄到數據庫,並將其分配給他。但是,當該用戶登錄到應用程序時,他看不到他的角色應該能夠看到的所有功能,f.x.一些按鈕或標籤。我沒有得到錯誤:註冊電子郵件中的ASP.NET連字符

User name [email protected] is invalid, can only contain letters or digits.

註冊時。

AllowOnlyAlphanumericUserNames設置爲false

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) 
    { 
     AllowOnlyAlphanumericUserNames = false, 
     RequireUniqueEmail = true 
    }; 
} 

,這適用於電子郵件包含一個點,而不是連字符([email protected]),用戶可以看到所有他的角色的用戶應能夠看到。

編輯

建議添加代碼到AccountController按照this question問題仍然存在之後。

+2

[ASP.NET MVC中帶有破折號的身份電子郵件]的可能重複(http://stackoverflow.com/questions/26726750/identity-email-with-dash-in-asp-net-mvc) – Liam

回答

0

最終解決我的問題是用戶使用用戶名和密碼登錄,而不是電子郵件和密碼。

Login.cshtml

@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" }) 
<div class="col-md-10"> 
    @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) 
    @Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" }) 
</div> 

而且在AccountController登錄POST方法設定的第一參數爲model.UserName:

var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false); 

此外,我添加了一個簡單的正則表達式屬性檢查,禁止用戶名中的連字符,因爲具有連字符的用戶名會導致相同的問題。

AccountViewModels.cs

[RegularExpression("[^-]+", ErrorMessage = "Username can not contain a hyphen (-)")] 

現在電子郵件仍然可以有連字符在它和所有角色相關的功能是登錄的用戶可見。