* get_roles: returns an array with the roles that have been associated to the current user;
* isUserInRole: receives a string and returns true if the user is associated with that role.
You will normally use the global Sys.Services.RoleService object to call these methods. To do that, you will need to start by activating the service. You can accomplish this by adding the necessary info to the web.config file:
<system.web.extensions>
<scripting>
<webServices>
<roleService enabled="true"/>
</webServices>
</scripting>
</system.web.extensions>
After doing this, if you drop a ScriptManager control on a page, you will automatically get some JS code inserted into the page that configures the client class so that it is able to communicate with the server side in order to retrieve the roles associated with the current user. It is important to note that this client service will only get the roles associated with the current user (which means that you need to have a user logged in to get any meaningful information).
So, if you build a really simple page like this one (I am just showing the body portion of the page):
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server" ID="manager">
</asp:ScriptManager>
</div>
</form>
</body>
and if you load the page on the browser and take a look at its source, you should find a line that looks like this:
Sys.Services._RoleService.DefaultWebServicePath = Role_JSON_AppService.axd;
This line is configuring the client class so that it knows which service it should call in the server side to call when you use one of the client methods presented earlier. You can take this to next level and automatically inject the roles associated with the current user on the page by setting the LoadRoles property of the RoleServiceManager to true (exposed by the RoleService property of the ScriptManager element):
<asp:ScriptManager runat="server" ID="manager">
<RoleService LoadRoles="true" />
</asp:ScriptManager>
When you do this, you will see that besides setting the path to the web service, the server side will also inject the roles associated with the current user with code similar to this one:
Sys.Services.RoleService._roles = Sys.Serialization.JavaScriptSerializer.deserialize(["Role1"]);
In my previous example, the current user had was associated with only one role (Role1).
Btw, I believe that it is important to mention that you can force the client object to load the roles from the client side by calling the load method. The next page shows a snippet that shows how this can be accomplished:
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server" id="manager">
<RoleService LoadRoles="false" />
</asp:ScriptManager>
<input type="button" value="Show roles" onclick="printRoles()" />
</div>
</form>
<script type="text/javascript">
Sys.Application.add_load( handleLoad );
var _loaded = false;
function handleLoad(){
Sys.Services.RoleService.set_defaultLoadCompletedCallback(
function(){
_loaded = true;
}
);
}
function printRoles() {
if(!_loaded){
Sys.Services.RoleService.load();
alert( "Loading roles...please try again in a few moments" );
return;
}
var arr = Sys.Services.RoleService.get_roles();
alert( arr );
}
</script>
</body>
In this case, we have a page with a single button that will be responsible for printing the roles associated with the current user. As you can see, we start by checking a global variable which will only be set after the roles associated with the current user have been loaded. we need this flag because when we call the load method, we will start an async operation and we need to be sure that we have already got the answer from the server side before printing a message to the user.
Before ending the post, there is still time to tell you that you can build your own Role web service: you just need to inform the client object that it should call your method instead of calling the default one. The easiest way to achieve this is to use the RoleServiceManager is Path property (though you can also force this by using the set_path property of the RoleService global object).
source: msmvps
Related Stuff
-
MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.
-
Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone
-
TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support
-
ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.
-
Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.
-
Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software
- PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.
Be the first ... |Add your comment.
Your Comment ...
Name (required)
Email (required, hidden)
Website
