Asynchronously calling the role service of application services

Source: Internet
Author: User

The ASP. Net role can be used to group Authenticated Users and treat each group as a unit,

To achieve unified management of multiple users,

Use Microsoft Ajax Library applicationsProgramRole Service

You can access the authenticated user role from the client script in a web application that supports Ajax,

The roleservice proxy class is used to complete this task,

This proxy class is automatically generated by the application role service and downloaded to the browser,

If there is a roleservice proxy class, that is, access the role service from the client JavaScript,

You must make the following settings,

First, you must ensure that your membership qualification data warehouse has been registered (aspnet_regsql.exe Registration)

Then you must ensure that you are using the forms authentication mode,

In addition, you must enable the role function in your application,

Finally, you must enable the roleservice function in Web. config,

Next we will introduce thisSYS. Services. roleserviceThis class

First, of course, it is the most important load method to introduce.

The complete method is named

SYS. Services. roleservice. Load (Loadcompletedcallback,

Loadfailedcallback, usercontext)

I will not explain the above parameters, that is, two callback functions,

What you need to explain this method is,

This load method loads the role of the authenticated user to the memory of the local client,

Then, you can use the roles attribute to access

The following describes the roles attributes,

SYS. Services. roleservice. Roles attributes

This attribute is used to obtain the role of the currently authenticated user,

Using get_roles () will get an array. When using the roles attribute,

You must first use the load method to load the role of the current user to the client memory,

Otherwise, the returned array is null.

Then there is the isuserinrole attribute.

It is used to check whether the authenticated user has a specified role,

SYS. Services. roleservice. get_isuserinrole (role );

If the authenticated user has a role specified by the role parameter,

True is returned; otherwise, false is returned.

This attribute is used in the same way as the SCO roles. You must load the role before using it,

Otherwise, false is always returned.

It also has the attributes path, timeout, defaultfailedcallback,

Defaultloadcompletedcallback, defasusucceededcallback,

Defaultusercontext and other attributes. I will not introduce them more, because of these attributes,

It is the same as calling Web service using JavaScript. If you cannot understand it,

Check msdn.

Here is the demo.

<% @ Page Language = "C #" %>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Style type = "text/CSS">
. Style1
{
Width: 35%;
Border: 1px solid # 8000ff;
Height: pixel PX;
}
. Style3
{
Width: 62px;
Height: 33px;
}
. Style5
{
Width: 62px;
Height: 39px;
}
. Style6
{
Height: 39px;
}
. Style7
{
Height: 33px;
}
</Style>

<Script language = "JavaScript" type = "text/JavaScript">

Function btnlogin_onclick (){
VaR username = $ get ('<% = txtname. clientid %>'). value;
VaR userpwd = $ get ('<% = txtpwd. clientid %>'). value;
SYS. Services. authenticationservice. login ( Username, userpwd,
False, null, null,
Logincompletedcallback,
Loginfailedcallback, "Logon ");
}

Function Logincompletedcallback (Result, usercontext, methodname ){
If ( Result = true ){
Alert ("Logon successful !!! ");
SYS. Services. roleservice. Load ( Loadcompletedcallback,
Loadfailedcallback, "load role ");
}
Else {
Alert ("the user name or password is incorrect !!! ");
}
}

FunctionLoginfailedcallback(Error, usercontext, methodname ){
Alert ("exception information:" + error. get_message ());
}

Function Loadcompletedcallback (Result, usercontext, methodname ){
VaR lblmsg = $ get ('<% = lblmsg. clientid %> ');
VaR MSG = "";
MSG + = "whether the current user belongs to the superadmin role ---" +
SYS. Services. roleservice. isuserinrole ("Superadmin") + "<br/> ";
MSG + = "whether the current user belongs to the admin role ---" +
SYS. Services. roleservice. isuserinrole ("Admin") + "<br/> ";
MSG + = "whether the current user belongs to the user role ---" +
SYS. Services. roleservice. isuserinrole ("User") + "<br/> ";
MSG + = "<HR/> ";
MSG + = "roles result: <br/>"
VaR rolearray = SYS. Services. roleservice. get_roles ();
For (VAR I = 0; I <rolearray. length; I ++ ){
MSG + = rolearray [I] + "<br/> ";
}
Lblmsg. innerhtml = MSG;
}

Function Loadfailedcallback (Error, usercontext, methodname ){
Alert ("exception information:" + error. get_message ());
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: Scriptmanager Id =" Scriptmanager1 "Runat =" server ">
</ASP: scriptmanager>
<Div>
<Table class = "style1">
<Tr>
<TD class = "style3">
User Name
</TD>
<TD class = "style7">
<Asp: Textbox Id =" Txtname "Runat =" server ">
</ASP: textbox>
</TD>
</Tr>
<Tr>
<TD class = "style5">
Password
</TD>
<TD class = "style6">
<Asp: Textbox Id =" Txtpwd "Runat =" server"
Textmode = "password">
</ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
& Nbsp;
</TD>
<TD>
<Input id =" Btnlogin "Type =" button "value =" login"
Onclick = "Return btnlogin_onclick ()"/>
</TD>
</Tr>
</Table>
<Br/>
<Asp: Label id =" Lblmsg "Runat =" server "forecolor =" # ff0066 ">
</ASP: Label>
<Br/>
<Br/>
<Br/>
<Br/>
</Div>
</Form>
</Body>
</Html>

Let's take a look at the effect.

Log On With Xiaozhen in the superadmin role

Log On With Suha in the admin role.

The above is the entire demo and demo. You can refer toCodePartially completed on your own !!!

2010-2-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.