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
- Admin
- Auditor
- 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; }
}
- Role Master Class
public class RoleMaster
{
[Key]
public int RoleID { get; set; }
public string RollName { get; set; }
}
- 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();
}
}
- 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; }
}
- 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>
}
- 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