Authentication Service for asynchronous calling of application services

Source: Internet
Author: User

What is authentication service?

In essence, this authentication service is used.

To verify the application stored in ASP. NET membership (membership)ProgramUsers in the service,

That is, a function to verify the legality of users,

Here is an introductionAsynchronousCall the authentication service,

But before that,

You must register the database,

Make certain settings in Web. config to successfully implement asynchronous calls and other settings,

To access the Authentication Service asynchronously.

First, register the website database, that is, use the aspnet_regsql.exe tool to complete registration,

You can view these content in my blog, which provides a detailed introduction,

Then, your website must be set to use the forms authentication mode,

This can be achieved using ASP. NET Website management tools,

You can also directly edit the Web. config file,

Then, enable the verification service in Web. config,

In this way, you can use the ASP. NET Ajax client architecture

Provided the authentication application service client proxy class to achieve login,

The main classes involved are:

SYS. Services. authenticationserviceThis proxy class

The following describes some of the main methods.

SYS. Services. authenticationservice. login ()This method is used to Implement Asynchronous login operations.

It has multiple parameters that need to be set. First, since it is login, there will certainly be username and userpwd,

Then there is the redirecturl page that needs to be redirected after successful login. Since it is implemented asynchronously,

Then there will certainly be two callback functions, namely, the verification is completed (not the logon is successful) and the verification fails,

That is, two callback functions: logincompletedcallback and loginfailedcallback,

Of course, like learning web service before, it will also pass in the user content, that is, usercontext,

There are two special parameters, ispersistent and custominfo,

Among them, the ispersistent attribute indicates whether the verified pass must span the session Status (session) of the browser)

Custominfo is reserved for future use. The default value is null,

Therefore, the complete login method is

SYS. Services. authenticationservice. login (username, userpassword,

Ispersistent, custominfo,

Redirecturl, logincompletedcallback,

Loginfaieldcallback, usercontext)

After the login method is introduced, the next step is to explain the logout, that is, the logout method,

To cancel the authentication, clear the authentication cookie in the browser,

To log out of users that have been verified,

The complete logout method is

SYS. Services. authenticationservice. logout (redirecturl,

Logoutcompletedcallback,

Logoutfailedcallback, usercontext)

The parameter is the URL of the page to be redirected after cancellation, and the other two are the callback function,

Because this blog post introduces authenticationservice,

Since it is verification, of course, the most important thing is login and cancellation, but there are some important attributes,

Next we will introduce these attributes one by one,

First

SYS. Services. authenticationservice. isloggedin attributes

This attribute is used to determine the current logon status. If you have logged on, it is true; otherwise, it is false,

Then

SYS. Services. authenticationservice. Timeout attribute

This attribute is used to obtain or set the authenticationservice timeout time.

Another one is

SYS. Services. authenticationservice. Path attribute

This attribute is used to obtain or set the authentication service path.

The attributes described below are used to set some attributes of the default success or failure callback function.

SYS. Services. authenticationservice. defaultfailedcallback attribute

This attribute is used to set or retrieve the name of the default failed callback function,

However, you must note that,

The Set callback function must have three parameters: error, usercontext, methodname,

I think everyone is very familiar with these attributes, because they are often used in some of my previous blog posts,

SYS. Services. authenticationservice. defaultsucceededcallback attribute

SYS. Services. authenticationservice. defaultlogincompletedcallback attribute

SYS. Services. authenticationservice. defaultlogoutcompletedcallback attribute

I will not introduce these three attributes much. I think we should see this attribute name and you will understand what it means,

If you do not understand it, check msdn.

(The demo below will not be demonstrated, but it will certainly be used in the future,

The content is relatively simple, so it will be explained later)

The following is a demo to complete the authentication and logout functions.

First, you must set the Web. config file to enable authenticationservice,

In addition, the forms authentication mode is used, and the website database is also registered,

See belowCodeNow

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

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Style type = "text/CSS">
. Style1
{
Width: 246px;
Border: 1px solid # 8000ff;
Height: 164px;
}
</Style>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: Scriptmanager Id = "scriptmanager1" runat = "server">
</ASP: Scriptmanager >
<Div style = "font-family:; font-size: Small">
<Table class = "style1">
<Tr>
<TD>
User Name
</TD>
<TD>
<Asp: Textbox Id =" Txtname "Runat =" server ">
</ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
Password
</TD>
<TD>
<Asp: Textbox Id =" Txtpwd "Runat =" server"
Textmode = "password"> </ASP: textbox>
</TD>
</Tr>
<Tr>
<TD style = "text-align: Center" colspan = "2">
<Asp: Label Id =" Lblmsg "Runat =" server "text =" not logged on"
Style = "color: # ff0066; font-size: Small"> </ASP: Label>
</TD>
</Tr>
<Tr>
<TD style = "text-align: Center">
& Nbsp;
< Input Id =" Btnlogin "Type =" button "value =" login"
Onclick = "btnlogin_login ()"/>
</TD>
<TD style = "text-align: Center">
< Input Id =" Btnlogout "Type =" button "value =" NOTE"
Onclick = "Return btnlogout_onclick ()"/>
</TD>
</Tr>
</Table>
</Div>
</Form>

<Script language = "JavaScript" type = "text/JavaScript">
Function btnlogin_login (){
VaR username = $ get ('<% = txtname. clientid %>'). value;
VaR userpwd = $ get ('<% = txtpwd. clientid %>'). value;
// Call the login Method for verification
SYS. Services. authenticationservice. login ( Username, userpwd,
False, null, null,
Onloginsucceededcallback,
Onloginfailedcallback, "login ");
}

// Verification Successful
function onloginsucceededcallback (result, usercontext, methodname) {
// valid user
If ( result = true ) {
alert ("Logon successful !!! ");
}< br> else {
// illegal user logon
alert (" incorrect user name or password !!! ");
}

// Use the isloggedin attribute to determine the current user logon status
VaR lblmsg = $ get ('<% = lblmsg. clientid %> ');
If (SYS. Services. authenticationservice.Get_isloggedin ()= True ){
Lblmsg. innertext = "logged on ";
}
Else {
Lblmsg. innertext = "not logged on ";
}
}

// Verification Failed
Function Onloginfailedcallback (Error, usercontext, methodname ){
VaR MSG = "";
MSG + = "exception message:" + error. get_message () + "\ n ";
MSG + = "exception type:" + error. get_exceptiontype () + "\ n ";
MSG + = "timeout information:" + error. get_timedout () + "\ n ";
MSG + = "stack trace:" + error. get_stacktrace () + "\ n ";
MSG + = "Status Code:" + error. get_statuscode () + "\ n ";
Alert (MSG );
VaR lblmsg = $ get ('<% = lblmsg. clientid %> ');
If (SYS. Services. authenticationservice. get_isloggedin () = true ){
Lblmsg. innertext = "logged on ";
}
Else {
Lblmsg. innertext = "not logged on ";
}
}

Function btnlogout_onclick (){
// Call the logout method to log out
SYS. Services. authenticationservice. logout (Null,
Onlogoutsucceededcallback,
Onlogoutfailedcallback, "logout ");
}

// Logout successful
Function Onlogoutsucceededcallback (Result, usercontext, methodname ){
Alert ("logout successful ");
VaR lblmsg = $ get ('<% = lblmsg. clientid %> ');
If (SYS. Services. authenticationservice. get_isloggedin () = true ){
Lblmsg. innertext = "logged on ";
}
Else {
Lblmsg. innertext = "not logged on ";
}
}

// Logout failed
Function Onlogoutfailedcallback (Error, usercontext, methodname ){
VaR MSG = "";
MSG + = "exception message:" + error. get_message () + "\ n ";
MSG + = "exception type:" + error. get_exceptiontype () + "\ n ";
MSG + = "timeout information:" + error. get_timedout () + "\ n ";
MSG + = "stack trace:" + error. get_stacktrace () + "\ n ";
MSG + = "Status Code:" + error. get_statuscode () + "\ n ";
Alert (MSG );
VaR lblmsg = $ get ('<% = lblmsg. clientid %> ');
If (SYS. Services. authenticationservice. get_isloggedin () = true ){
Lblmsg. innertext = "logged on ";
}
Else {
Lblmsg. innertext = "not logged on ";
}
}
</SCRIPT>

</Body>
</Html>

The following figure shows the results. In my data tables aspnet_users and aspnet_membership

The Xiaozhen user exists. I will use this user as an example.

Logout

The above is a complete demonstration of authenticationservice.

If you do not know anything, you can check msdn to solve the problem.

2010-2-11

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.