微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基于ASP .NEt MVC用户配置文件的登录用户,并根据配置文件重定向到指定URL

如何解决基于ASP .NEt MVC用户配置文件的登录用户,并根据配置文件重定向到指定URL

需要基于个人资料的登录帮助。我创建了登录操作,但是无法根据用户的个人资料将用户定向到特定的URL。下面如果我的代码 遵循创建的角色

  1. 管理员
  2. 审计员
  3. 员工

1。)用户类别

public class User
    {
        [Key]
        public int Id { get; set; }
        [required]
        public string Username { get; set; }
        [required]
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
     }
  1. 角色大师班
public class RoleMaster
    {
        [Key]
        public int RoleID { get; set; }
        public string RollName { get; set; }
    }
  1. UserRoleProvider类
    public class UserRoleProvider : RoleProvider
    {
        public override string ApplicationName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        public override void AddUsersToRoles(string[] usernames,string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName,bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName,string usernametoMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            using (Db context = new Db())
            {
                var userRoles = (from user in context.Users
                                 join roleMapping in context.UserRolesMappings
                                 on user.Id equals roleMapping.Id
                                 join role in context.RoleMasters
                                 on roleMapping.RoleID equals role.RoleID
                                 where user.Username == username
                                 select role.RollName).ToArray();
                return userRoles;
            }
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username,string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromroles(string[] usernames,string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }

  1. UserRoleMapping类
public class UserRolesMapping
    {
        [Key]
        public int URMID { get; set; }
        public int Id { get; set; }
        public int RoleID { get; set; }

        [ForeignKey("Id")]
        public virtual User Users { get; set; }
        public IEnumerable<SelectListItem> User { get; set; }

        [ForeignKey("RoleID")]
        public virtual RoleMaster RoleMasters { get; set; }
        public IEnumerable<SelectListItem> RoleMaster { get; set; }
    }
  1. 登录表单
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()


            <div class="col-sm-9 col-md-7 col-lg-5 mx-auto">
                <div class="card card-signin my-5">
                    <div class="card-body">
                        <h5 class="card-title text-center">Ticketool</h5>
                        @Html.ValidationSummary(true,"",new { @class = "text-danger" })
                        <form class="form-signin">
                            <div class="form-group">
                                @Html.EditorFor(model => model.Username,new { htmlAttributes = new { @class = "form-control col-md-12",@placeholder="Username here.." } })
                                @Html.ValidationMessageFor(model => model.Username,new { @class = "text-danger" })
                            </div>

                            <div class="form-group">
                                @Html.PasswordFor(model => model.Password,new { @class = "form-control col-md-12",@placeholder="Password here.."  })
                                @Html.ValidationMessageFor(model => model.Password,new { @class = "text-danger" })
                                
                            </div>

                            <button class="btn btn-lg btn-primary btn-block text-uppercase" type="submit">Sign in</button>
                            <hr class="my-4">

                        </form>
                    </div>
                </div>
            </div>
}
  1. 发送登录请求的控制器
[HttpPost]
        [ValidateAntiForgeryToken]
        [AllowAnonymous]
        public ActionResult Login(User model)
        {
            if (ModelState.IsValid)
            {
                using (Db db = new Db())
                {

                    bool isValidUser = db.Users.Any(x => x.Username == model.Username && x.Password == model.Password);

                    if (isValidUser)
                    {
                        
                        FormsAuthentication.SetAuthCookie(model.Username,false);
                        return RedirectToAction("MyTickets","Ticket");

                    }
                    else
                    {

                        bool isUserValid = db.Users.Any(x => x.Username != model.Username && x.Password != model.Password);
                        if (isUserValid)
                        {
                            ModelState.AddModelError("","Username / Password is Invalid");
                            return View(model);
                        }
                    }
                }
            }
            return View();
        }

非常感谢您的帮助

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。