我正在使用ASP.NET身份,我在代码块中获取null validator = manager.UserManager的空值,我想将allowonlyalfanumericusernames更改为false,因为我无法在用户名中使用č,ć,š,ž进行注册
以下是代码:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using System.Data.Entity;
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["SeminariEFConnectionString"].ConnectionString;
UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
var validator = manager.UserValidator as UserValidator<IdentityUser>;
if (validator != null) validator.AllowOnlyAlphanumericUserNames = false;
IdentityUser user = new IdentityUser();
user.UserName = txtKorisnicko.Text;
if (txtPass.Text == txtPassOK.Text)
{
try
{
IdentityResult result = manager.Create(user, txtPass.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.Message.ToString();
}
}
else
{
litStatus.Text = "Lozinke moraju biti identične !";
}
}
}
现在有人有什么问题,为什么我会为验证器获取空值?
答案 0 :(得分:-1)
您可以在UserManager构造函数中更改它,而不是稍后更改它。
UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };