0

Need help with login based on profile. I created login action , but unable to direct user to specific urls based on their profile. Below if my code Following Roles created

  1. Admin
  2. Auditor
  3. Employee

1.) User Class

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. Role Master Class
public class RoleMaster
    {
        [Key]
        public int RoleID { get; set; }
        public string RollName { get; set; }
    }
  1. UserRoleProvider Class
    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 Class
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. Login form
@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. Controller to send login request
[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();
        }

Help is much appreciated

Riyaz Shaikh
  • 83
  • 11
  • Take a look at [this](https://stackoverflow.com/questions/32260728/asp-net-mvc-login-and-redirect-based-on-role) – Azhar Hussain Sep 24 '20 at 11:03
  • [this](https://stackoverflow.com/questions/26526536/asp-net-mvc-5-identity-2-login-redirect-based-on-user-role) and [this](https://stackoverflow.com/questions/35937186/redirect-users-according-to-their-identity-role-asp-mvc-4-and-5) – Azhar Hussain Sep 24 '20 at 11:04
  • Does this answer your question? [ASP.NET MVC Login and Redirect Based On Role](https://stackoverflow.com/questions/32260728/asp-net-mvc-login-and-redirect-based-on-role) – Sameer Kumar Jain Sep 24 '20 at 11:43
  • no success, tried the above options – Riyaz Shaikh Sep 27 '20 at 11:42

0 Answers0