0

I am using asp.net mvc4 to create authentication and authorization application ,and i have created a common login page for all users in different roles(authentication and authorization is based on role),it should ask for username and password and when we click on login button it should check the user's role in database,and i am checking that as in below code

But here its not entering in the if part,and also i have checked using add watch so it say's: IsUserInRole -The name 'IsUserInRole' does not exist in the current context

I am using simple membership provider and want to built application using built-in methods and i have configured all in web.config

I am not able to figure whats the problem,i have read/searched many articles,any help would be greatly appreciated, pls help me.

if(Roles.IsUserInRole(WebSecurity.CurrentUserName,"Admin")) 
{
  RedirectToAction("Admin","Home");
}
codemaniac143
  • 1,241
  • 2
  • 11
  • 18

1 Answers1

2

IsUserInRole sits in the System.Web.Security namespace and you'll have to reference System.Web.dll to use it.

http://msdn.microsoft.com/en-us/library/4z6b5d42.aspx

Erwin
  • 3,060
  • 26
  • 24
  • @codemaniac143 Did you read http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx – Erwin Oct 03 '13 at 07:53
  • yes i read. and now i am trying to use Roles.GetRolesForUser(User.Identity.Name),so even for this it says:The GetRolesForUser is does not exist in the current context.so all this methods are not working in my login controller,right? – codemaniac143 Oct 03 '13 at 09:16
  • I have added role in wepages_Roles table using one of the built in method Roles.CreateRole(),i have written the code for the same in global.asx,their this methods are working fine,then why not in login controller,whats wrong??!!..pls help – codemaniac143 Oct 03 '13 at 09:38
  • Not sure about this ... tried google and found the following hits, maybe you read them, maybe you didn't, http://stackoverflow.com/questions/12257232/role-based-authentication-in-the-new-mvc-4-internet-template-using-simplemembers, http://stackoverflow.com/questions/14527152/how-exactly-do-i-use-roles-in-asp-net-mvc-4, http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/, http://odetocode.com/blogs/scott/archive/2012/09/24/perils-of-the-mvc4-accountcontroller.aspx – Erwin Oct 03 '13 at 09:57
  • thanks @Erwin,i have gone through all this links before,but didnt work – codemaniac143 Oct 03 '13 at 10:12
  • If you debug, and look at the Roles is the Provider of type WebMatrix.WebData.SimpleRoleProvider? – Erwin Oct 03 '13 at 11:04
  • if i got you right you mean Roles in itself is different provider and not of the type simpleroleprovider,even i think yes.but i tried creating object of SimpleRoleProvider and invoking method r.IsInRole,for that also it says IsInRole does not exist in current context – codemaniac143 Oct 03 '13 at 11:32
  • I mean Roles.Provider. – Erwin Oct 03 '13 at 11:37
  • If i am using Roles.Provider..it says the name Provider does not exist in current context – codemaniac143 Oct 03 '13 at 11:46
  • In debug, what type is Roles? System.Web.Security.Roles? – Erwin Oct 03 '13 at 12:04
  • Did you check out this link: http://aaron-hoffman.blogspot.be/2013/02/aspnet-mvc-4-membership-users-passwords.html – Erwin Oct 03 '13 at 12:24
  • thanks,but after checking this still the condition is same and where u would write all this code to check the user's role???,may be i am making some mistakes – codemaniac143 Oct 03 '13 at 13:40
  • I tried,i am trying,but my problem is not yet solved,somebody pls help – codemaniac143 Oct 04 '13 at 05:04
  • Try starting from a clean Internet MVC 4 Application and put something like if (Roles.IsUserInRole("Admin")) {} in your Login (post), check the type of Roles and Roles.Provider. – Erwin Oct 04 '13 at 06:06
  • ,i created a new application and tried what you said,still same problem with IsUserInrole – codemaniac143 Oct 04 '13 at 07:18
  • Strange, when I create a new Internet MVS 4 app, the Roles is of type System.Web.Security.Roles and the Provider is of type WebMatrix.WebData.SimpleRoleProvider. I haven't changed anything to the project except for adding the if (Roles.IsUserInRole("Admin")) {} in my Login (post). – Erwin Oct 04 '13 at 08:38
  • Yes, as I explained in the previous comment. – Erwin Oct 08 '13 at 06:20
  • can you pls share your web.config lines – codemaniac143 Oct 08 '13 at 06:52
  • That was my point, when you create a new MVC 4 app, there is nothing in the config about membership and rolemanager. SimpleRoleProvider is the default, there is a InitializeSimpleMembershipAttribute that initializes your database connection (DefaultConnection). – Erwin Oct 08 '13 at 07:27
  • Is their anyone ,who can help me to get out of the problem,i have spent much time on solving the problem,but i am not able to get out of it – codemaniac143 Oct 08 '13 at 10:30
  • ,can u pls tell ,exactly what you did – codemaniac143 Oct 09 '13 at 08:14
  • I made a new project, MVC 4, chose the internet template, added connectionstring for the DefaultConnection in the web.config and pointed it to a local SQLExpress Sql instance. – Erwin Oct 09 '13 at 11:24
  • i want to hide some action methods in UI to some users based on their roles,how to do that???? – codemaniac143 Oct 16 '13 at 05:00