当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - 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?

网友答案:
if (User.IsInRole("rolename")) {
  // what you wan't to do.
}
网友答案:

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.

网友答案:

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.
}
网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: