简体   繁体   English

无法登录,但可以在 ASP.NET MVC 5 应用程序中注册

[英]Can't login, but can register in ASP.NET MVC 5 app

After succesfully registering a random user, the app isn't logging them authomatically.成功注册随机用户后,应用程序不会自动记录他们。 I can't even login after typing in infos in defaulf Login View.在默认登录视图中输入信息后,我什至无法登录。 It just directs me to Home/Index View and that's all.它只是将我引导至主页/索引视图,仅此而已。 I've added AuthorizeAttribute in FilterConfig and an Admin Role我在 FilterConfig 中添加了 AuthorizeAttribute 和一个管理员角色

I tried to create plain new example ASP.NET MVC 5 app just to test the default logging, but it didn't work there either.我试图创建简单的新示例 ASP.NET MVC 5 应用程序只是为了测试默认日志记录,但它也没有在那里工作。 As in previous app it just keeps directing me to Home/Index page与之前的应用程序一样,它一直将我引导至主页/索引页面

    // GET: /Account/Login
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    //
    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {

            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Nieprawidłowa próba logowania.");
                return View(model);
        }
    }

    [AllowAnonymous]
    public ActionResult Register()
    {
        return View();
    }

    //
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {

                //var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext());
                //var roleManager = new RoleManager<IdentityRole>(roleStore);
                //await roleManager.CreateAsync(new IdentityRole { Name = "Admin" });
                //await UserManager.AddToRoleAsync(user.Id, "Admin");

                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);



                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        return View(model);
    }

I'd like to know how do I login the users successfully.我想知道如何成功登录用户。

Looking at your code, someone would wonder how it is not throwing an error already or how a Build completed.查看您的代码,有人会想知道它是如何没有抛出错误或构建如何完成的。

in your Account controller the Login [HttpPost], The line below is not there.在您的Account controller Login [HttpPost],下面的行不存在。

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

You need to have this line after declaring the variable声明变量后你需要有这一行

switch (result)
{
     //ommited block of code
}

In case someone experience this issue, make sure of the model view, it has to be matched (each property should be rendered in the view) unless if the property is defaulted.如果有人遇到此问题,请确保模型视图必须匹配(每个属性都应在视图中呈现),除非该属性是默认值。 Along with checking properties types.随着检查属性类型。

Next, you need to check the controller and the view, ensure that the number of passed properties is correct.接下来,您需要检查控制器和视图,确保传递的属性数量正确。

mismatched mapping would cause failure action.不匹配的映射会导致操作失败。

Mostly this would solve the issue.大多数情况下,这将解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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