ASP.NET MVC and Login Authentication

后端 未结 4 1126
长发绾君心
长发绾君心 2021-01-30 03:22

I have searched many posts here regarding custom user authentication but none have addressed all of my concerns

I am new to ASP.NET MVC and have used traditional ASP.NET

4条回答
  •  隐瞒了意图╮
    2021-01-30 04:25

    1-Add This Code To WebConfig

    
    
           
           
           
    
    
    

    2-To Action Use This code

    [HttpPost]
    public async Task Login(string UserName,string Password)
    {
        var q = await userpro.Login(UserName, Password);
        if (q.Resalt)
        {
    
            //Add User To Cookie
            Response.Cookies.Add(FormsAuthentication.GetAuthCookie(UserName, false));
    
            return RedirectToAction("ShowUsers", "User");
        }
        else
        {
            ViewBag.Message = q.Message;
            return View();
        }
    
    }
    

    3-You Should Add This Attribute To Your Action [Authorize]

    4-To This Code You Can Get UserName In Cookie

    public async Task ShowUsers(int Page = 0)
    {
        string UserName= User.Identity.Name;
        return View(await user.GetAllUser(Page));
    }
    

提交回复
热议问题