0

I have added everything which i needed for this,But still its is not working (am first time with Identity)

Here is my code:

Controller

  [Authorize(Roles = "Admin")]
    public class AdminController : Controller
    {
     //Codes in the controller

}

MyRoleProvider

  public override string[] GetRolesForUser(string username)
        {
         var result=adminService.LoginData();//which has Role,and other datas
         string a=result.Where(x=>x.UserName==username).FirstOrDefault().Role;
         string[] output={a};
         return output;

        }

Web.Config

<roleManager enabled="true" defaultProvider="MyProvider">
  <providers>
    <clear/>
    <add name="MyProvider" type="bombays.MyRoleProvider"/>
      </providers>
    </roleManager>

Login like this

 public ActionResult Login(Login log)
        {
            var p = adminService.adminLogin(log);

            if (p.UserName == log.UserName && p.Password == log.Password)
            {
                Session["adminusername"] = p.UserName;
                Session["adminname"]=p.name;
            }
            return View("AdminHome");
        }

I have checked lot of example but nothing saves me. Now what is happening is ;Any user can view the page.Its not even calling GetRolesForUser function(tried with breakpoint)

John Conde
  • 217,595
  • 99
  • 455
  • 496
codeseeker
  • 196
  • 1
  • 13
  • What part of this isn't working? Be more specific. – Train Dec 13 '19 at 20:42
  • Its not even calling GetRolesForUser function – codeseeker Dec 13 '19 at 20:44
  • I don't see you calling it anywhere in your code. Where are you setting the attribute? – Train Dec 13 '19 at 20:45
  • Oops.Sorry just updated the question – codeseeker Dec 13 '19 at 20:47
  • Like most comments are saying you need to make the call to GetRolesForUser somewhere. What type of authentication are you using? – Deleted Dec 13 '19 at 20:50
  • Direct me to the blog post you used to implement this please. – Train Dec 13 '19 at 20:50
  • https://stackoverflow.com/questions/19237285/using-asp-net-identity-in-mvc-4 – codeseeker Dec 13 '19 at 20:51
  • also a tutorial video referenced from YouTube .I have installed those3 packages in my project then the code which i have tried – codeseeker Dec 13 '19 at 20:52
  • You're not calling the function anywhere. Also, Are you talking about `Microsoft.AspNet.Identity.Core`? Why did you install that package for asp.net mvc 4? Edit your question and lay out all the steps you took. `step 1 ...` then I did `step 2...` Because right now we can't help you, this code isn't enough for me to see what's wrong. – Train Dec 13 '19 at 21:02
  • Can you help me how to solve this in another way? – codeseeker Dec 14 '19 at 07:08

0 Answers0