student
student

Reputation: 11

How to get a role using asp.net 4.0

I am new in ASP.NET 4.0 and C#..If I want hide/show menu item based on user logged in using web.sitemap, I must use a role and set it in web.config..I want to ask, where I can get that role?

Upvotes: 0

Views: 562

Answers (4)

user1567453
user1567453

Reputation: 2095

If I understand correctly, you are saying that you have an asp.net web application project open and you want to know how to create a user and assign a role to them.

If you currently have web.config configured to use the default provider for rolemanager and membership provider then asp.net will take care of all the tricky stuff. The only things you have to do is go to your menu bar and select "Project -> ASP.NET Configuration". This will bring up a GUI for creating users and roles then assigning them. It should be a pretty self explanatory tool. That should meet your web.config use requirement too.

As suggested above from here if you have the user logged in you can do things like:

if (User.IsInRole("rolename")) { // what you wan't to do. }

Alternatively (and best for you atm) you should try using the logInView control in your toolbox.(I think that's what it's called) Do some googling on using these controls and it'll get you across the line.

Upvotes: 0

vindh123
vindh123

Reputation: 144

You need to use IPrincipal to store roles.

GenericIdentity userIdentity = new GenericIdentity((FormsIdentity)HttpContext.Current.User.Identity.Name);

string[] roles = { "rolename1", "rolename2", "rolename3" };
GenericPrincipal userPrincipal = new GenericPrincipal(userIdentity, roles);
Context.User = userPrincipal;

then you can check for user roles

if (User.IsInRole("rolename1")) {
  // what you wan't to do.
}

Upvotes: 0

opewix
opewix

Reputation: 5083

If you are really new to ASP.NET, you need to learn about Users and Roles. Try to use Membership API with standard elements like "Login". After that you have to write your own Users and Roles Provider with custom data structure. Then, use @Randolf R-F statement.

Upvotes: 0

Erre Efe
Erre Efe

Reputation: 15557

if (User.IsInRole("rolename")) {
  // what you wan't to do.
}

Upvotes: 2

Related Questions