ASP中的用户管理器.. NET标识为NULL

本文关键字:标识 NULL NET 管理器 用户 ASP | 更新日期: 2023-09-27 18:12:27

我使用的是ASP。. NET身份,我正在为变量验证器=管理器获得空值。UserManager在代码块中,我想将allowonlyalfanumericusernames更改为false,因为我不能在注册时使用用户名中的je, je, je, je。

代码如下:

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 !";
        }
    }
}

有没有人现在是什么问题,为什么我得到空值验证器?

ASP中的用户管理器.. NET标识为NULL

您可以像这样在UserManager构造函数中更改它,而不是稍后更改。

UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };