i am verify that either particular person that whether it is admin or ordinary user
using two tables.
webpages_Roles
roleid Rolename
1 Admin
2 User
users
username fullname Emailid password rollid
Viks Admin a@gmail.com 1
my function
public static string IsUserInRole(string username)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mystring"].ToString()))
{
string str = "select b.RoleName from Users as a inner join webpages_Roles as b on a.RoleId = b.RoleId where UserName = @UserName";
var result = con.Query(str, new { username = username}).SingleOrDefault();
if (result == "Admin")
{
return "yes";
}
else
{
return "No";
}
}
I am calling
in my controler
string y = Roles.IsUserInRole(t.Name);
always i am geting no in variable
What I have tried:
[HttpPost]
public ActionResult login(FormCollection form)
{
Login t = new Login();
t.Name = form["Name"];
t.pass = form["Pass"];
if (ModelState.IsValid)
{
bool result = Accountdata.viks(t.Name, t.pass);
var user_id = Accountdata.GetUserID_By_UserName(t.Name);
var login_type = Accountdata.GetRoleByUserID(user_id);
if (result == true)
{
if (string.IsNullOrEmpty(login_type))
{
ModelState.AddModelError("Error!!", "Not authorized user!!");
return View(t);
}
else
{
Session["user_id"] = user_id;
Session["login_type"] = login_type;
Session["name"] = t.Name;
}
}
}
else
{
ModelState.AddModelError("Error!!", "not valid user!!");
return View(t);
}
string y = Roles.IsUserInRole(t.Name);
if (y == "Yes")
{
return RedirectToAction("AdminDashboard", "Dashboard");
}
else
{
return RedirectToAction("UserDashboard", "Dashboard");
}
}