ASP.NET标识AllowOnlyAlphanumericUserNames

时间:2016-10-10 10:44:13

标签: asp.net entity-framework asp.net-identity

有人可以帮助我如何在ASP.Net身份中使用特殊字符? 问题是我的用户无法注册特殊字符:č,ć,š,ž,đ。 当您尝试在注册时输入此字符时,我会收到以下错误: 用户名Krešo无效,只能包含字母或数字。

我在哪里以及如何更改此内容。

以下是代码:

using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_Account_Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnPrijava_Click(object sender, EventArgs e)
    {
        UserStore<IdentityUser> userStore = new UserStore<IdentityUser>();

        userStore.Context.Database.Connection.ConnectionString =
            System.Configuration.ConfigurationManager.ConnectionStrings["SeminariConnectionString3"].ConnectionString;

        UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);

        IdentityUser user = new IdentityUser();

        user.UserName = txtKorisnickoIme.Text;

        if(txtLozinka.Text == txtPotvrdaLozinke.Text)
        {

            try
            {
                IdentityResult result = manager.Create(user, txtLozinka.Text);

                if(result.Succeeded)
                {
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties(), userIdentity);

                    Response.Redirect("Pocetna.aspx");

                }

                else
                {
                    litStatus.Text = result.Errors.FirstOrDefault();

                }
            }
            catch (Exception ex)
            {

                litStatus.Text = ex.ToString();
            }
        }

        else
        {

            litStatus.Text = "Lozinke moraju biti identične.";
        }


    }
}

1 个答案:

答案 0 :(得分:1)

您应该可以按如下方式更改此行为:

var manager = new UserManager<IdentityUser>(userStore);  // existing code
var validator = manager.UserValidator as UserValidator<ApplicationUser>;
if (validator != null) validator.AllowOnlyAlphanumericUserNames = false;

validator应该为null,然后调试一下以找到在运行时使用的实际类型。