简体   繁体   中英

User manager in ASP.NET Identity is NULL

I am using ASP.NET identity and i am getting null value for variable validator = manager.UserManager in code block where i want to change allowonlyalfanumericusernames to false, because i cannot use č,ć,š,ž in username at registering.

Here is the code:

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

Does anyone now what is the problem, why do i get null value for validator?

您可以像这样在UserManager构造函数中对其进行更改,而不必稍后进行更改。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM