输入不是有效的Base-64字符串,因为它包含非基本64字符的ASP.NET标识

时间:2021-09-06 18:30:22

I am using asp.net identity for login and authentication. I am getting error for password field when login with following method

我使用asp.net身份进行登录和身份验证。使用以下方法登录时,我收到密码字段错误

model= is user entity

model =是用户实体

var user = await UserManager.FindAsync(model.UserName, model.Password);
                if (user != null)
                {
                    bool isPersistent = false;
                    await SignInAsync(user, isPersistent);
                    return RedirectToLocal(returnUrl);
                }

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters

输入不是有效的Base-64字符串,因为它包含非基本64个字符,两个以上的填充字符或填充字符中的非法字符

1 个解决方案

#1


0  

Are you using EF? If so, you shouldnt add AspNetRoles, AspNetUserClaims, AspNetUserLogins and AspNetUserRoles on your edmx.

你在用EF吗?如果是这样,你不应该在你的edmx上添加AspNetRoles,AspNetUserClaims,AspNetUserLogins和AspNetUserRoles。

In addition, I always use "Login" method like below. If I need to keep some info (like userRole, userName etc), I use Session like below.

另外,我总是使用如下的“登录”方法。如果我需要保留一些信息(比如userRole,userName等),我会使用如下的Session。

public async Task<ActionResult> Login(LoginViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var user = await UserManager.FindAsync(model.Email, model.Password);
        //var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        if (user != null)
        {
            string userRole = UserManager.GetRoles(user.Id).FirstOrDefault();
            Session["userRole"] = userRole;
            Session["userName"] = model.Email;
            await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); 
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", "Invalid username or password"); 
            return View(model);
        }
    }

Hence can you try

你能试试吗?

await SignInManager.PasswordSignInAsync

or

await SignInManager.SignInAsync

#1


0  

Are you using EF? If so, you shouldnt add AspNetRoles, AspNetUserClaims, AspNetUserLogins and AspNetUserRoles on your edmx.

你在用EF吗?如果是这样,你不应该在你的edmx上添加AspNetRoles,AspNetUserClaims,AspNetUserLogins和AspNetUserRoles。

In addition, I always use "Login" method like below. If I need to keep some info (like userRole, userName etc), I use Session like below.

另外,我总是使用如下的“登录”方法。如果我需要保留一些信息(比如userRole,userName等),我会使用如下的Session。

public async Task<ActionResult> Login(LoginViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var user = await UserManager.FindAsync(model.Email, model.Password);
        //var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        if (user != null)
        {
            string userRole = UserManager.GetRoles(user.Id).FirstOrDefault();
            Session["userRole"] = userRole;
            Session["userName"] = model.Email;
            await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); 
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", "Invalid username or password"); 
            return View(model);
        }
    }

Hence can you try

你能试试吗?

await SignInManager.PasswordSignInAsync

or

await SignInManager.SignInAsync