简体   繁体   English

如何验证用户身份以及如何根据登录人获取用户的名字和第二名?

[英]How can I authenticate user and how can I get first name and second name of user according to the login person?

Below is my login function, I want to change the following:下面是我的登录function,我想改一下:

  1. differentiate between admin login and user login to show different page layouts区分管理员登录和用户登录以显示不同的页面布局
  2. get the user's first name and second name according to the login user根据登录用户获取用户的名字和名字

please how can I do that any suggestion or examples?请问我该怎么做任何建议或例子?

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(Customers customer)
    {
        if (ModelState.IsValid)
        {
            var user = await _context.Customers.SingleOrDefaultAsync(m => m.Email == customer.Email && m.Password == customer.Password);
            if (user != null)

            {
                

                return RedirectToAction("Account", "Account");
            }
            else 
            {
                ViewBag.error = "Login Failed";
                return RedirectToAction("Login");
            }
        }
        else
        {
            
            return RedirectToAction("Index", "Home");
        }
    }
  • You need to maintain Role column values in login table in DB.您需要在数据库的登录表中维护角色列值。
  • Add values to the Role column Ex: Admin Role - 1, User - 0向角色列添加值 例如:管理员角色 - 1,用户 - 0
  • Change the login procedure to return the Role,FirstName,LastName instead of count更改登录过程以返回 Role、FirstName、LastName 而不是 count
Select Role,Password,FirstName,LastName from Users where Username=@Username and Password=@Password
  • After authenticating the user(valid authentication, if password matches), redirect to the View based on the role.验证用户后(有效验证,如果密码匹配),根据角色重定向到视图。
if (Validate_User(Customers customer))
{
    if (customer.Role == "1")
    {
        return View("AdminView");
    }
    else
    {
        return View("UserView");
    }
    ViewBag.FirstName=customer.FirstName;
    ViewBag.LastName=customer.LastName;
}
else
{
    //handle failed login    
     ViewBag.error = "Login Failed";
     return RedirectToAction("Login");
}

Please refer Role Based Access请参考基于角色的访问

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

相关问题 如何认证用户? - How can I authenticate a user? 如何通过登录 ActivityDirectory 获取名称? - How can I get name by login in ActivityDirectory? 如何获取登录用户名的客户端计算机 - How can i get the Logged in user Name of Client machine 如何使用 C# 在 Active Directory 中搜索用户的姓氏和名字 - How can I search for both the user's last name AND the first name in the Active Directory using C# 如何验证SharePoint用户凭据 - How can I authenticate SharePoint user credentials 在ASP.Net站点中,如何获取该用户登录的用户名? - In ASP.Net site, how do I get name of the user that the person is signed in as? 你好,我如何将 &amp;&amp; 添加到文本框和用户名和密码字符串,以及如何关闭第一个表单编辑 - Hello,how can i add && to a textbox and a string for user name and password,and how to close the first form Edited 如何使用 UserManager 按 ID、名称、email 或其他搜索条件获取用户(不是当前登录的用户)? - How can I get a user (not the currently logged in user) by Id, name, email or other search criteria, using UserManager? 如何在Wamp Server中存储的Xamarin中对用户登录信息进行身份验证 - How can I authenticate user login info in xamarin that is stored in wamp server 如何获取用户的网络登录名? - How can I get the users network login name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM