[英]User manager in ASP.NET Identity is NULL
我正在使用ASP.NET身份,並且代碼塊中的變量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 !";
}
}
}
有人現在有什么問題嗎,為什么我要為驗證器獲取空值?
您可以像這樣在UserManager構造函數中對其進行更改,而不必稍后進行更改。
UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.