Compartilhar via


Sys.Services.AuthenticationService Class

Provides the client proxy class for the authentication service.

Namespace: Sys.Services

Inherits: None

Members

Name

Description

Sys.Services.AuthenticationService Constructor

Initializes a new instance of the Sys.Services.AuthenticationService class.

Sys.Services.AuthenticationService.DefaultWebServicePath Field

Specifies the path of the default authentication service.

Sys.Services AuthenticationService login Method

Authenticates the user's credentials.

Sys.Services AuthenticationService logout Method

Logs out the currently authenticated user.

Sys.Services AuthenticationService defaultFailedCallback Property

Gets or sets the name of the default failed callback function.

Sys.Services AuthenticationService defaultLoginCompletedCallback Property

Gets or sets the name of the default login-completed callback function.

Sys.Services AuthenticationService defaultLogoutCompletedCallback Property

Gets or sets the name of the default logout-completed callback function.

Sys.Services AuthenticationService defaultSucceededCallback Property

Gets or sets the default succeeded callback function for the service.

Sys.Services AuthenticationService defaultUserContext Property

Gets or sets the default user context for the service.

Sys.Services AuthenticationService isLoggedIn Property

Gets the authentication state of the current user.

Sys.Services AuthenticationService path Property

Gets or sets the authentication service path.

Sys.Services AuthenticationService timeout Property

Gets or sets the authentication service time-out.

Remarks

The AuthenticationService class provides script access to user authentication. It calls methods of the authentication service through the same infrastructure used to call any other Web service method.

Note

The built-in authentication service is located on the server in a predefined location.

The AuthenticationService class is a singleton; it has only one instance with a global point of access. It is always available to your application and you do not have to instantiate it.

Example

Description

The following example shows how to use the AuthenticationService class to check whether a user is authenticated. For more information, see Using Forms Authentication with Microsoft Ajax.

Code

// Define global variables.
var usernameEntry;
var passwordEntry;
var username;
var password;
var textLoggedIn;
var textNotLoggedIn;
var buttonLogin;  
var buttonLogout; 

function pageLoad()
{
    usernameEntry = $get("NameId");
    passwordEntry = $get("PwdId");
    username = $get("username");
    password = $get("password");
    textLoggedIn = $get("loggedin");
    textNotLoggedIn = $get("notloggedin");
    buttonLogin = $get("ButtonLogin");  
    buttonLogout = $get("ButtonLogout"); 
}



// This function sets and gets the default
// login completed callback function.
function SetDefaultLoginCompletedCallBack()
{
    // Set the default callback function.
    Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(OnLoginCompleted);

    // Get the default callback function.
    var callBack =     
        Sys.Services.AuthenticationService.get_defaultLoginCompletedCallback();
}

// This function sets and gets the default
// logout completed callback function.
function SetDefaultLogoutCompletedCallBack()
{
    // Set the default callback function.
    Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback(OnLogoutCompleted);

    // Get the default callback function.
    var callBack =     
        Sys.Services.AuthenticationService.get_defaultLogoutCompletedCallback();
}


// This function sets and gets the default
// failed callback function.
function SetDefaultFailedCallBack()
{
    // Set the default callback function.
    Sys.Services.AuthenticationService.set_defaultFailedCallback(OnFailed);

    // Get the default callback function.
    var callBack =     
        Sys.Services.AuthenticationService.get_defaultFailedCallback();  
}


// This function calls the login method of the
// authentication service to verify 
// the credentials entered by the user.
// If the credentials are authenticated, the
// authentication service issues a forms 
// authentication cookie. 
function OnClickLogin() 
{   

    // Set the default callback functions.
    SetDefaultLoginCompletedCallBack();
    SetDefaultLogoutCompletedCallBack();
    SetDefaultFailedCallBack();

    // Call the authetication service to authenticate
    // the credentials entered by the user.
    Sys.Services.AuthenticationService.login(username.value, 
        password.value, false,null,null,null,null,"User Context");
}

// This function calls the logout method of the
// authentication service to clear the forms 
// authentication cookie.
function OnClickLogout() 
{  
   // Clear the forms authentication cookie. 
   Sys.Services.AuthenticationService.logout(null, 
        null, null, null); 
} 



// This is the callback function called 
// if the authentication fails.      
function OnFailed(error, 
    userContext, methodName)
{           
    // Display feedback message.
    DisplayInformation("error:message = " + 
        error.get_message());
    DisplayInformation("error:timedOut = " + 
        error.get_timedOut());
    DisplayInformation("error:statusCode = " + 
        error.get_statusCode());            
}


// The callback function called 
// if the authentication completed successfully.
function OnLoginCompleted(validCredentials, 
    userContext, methodName)
{
    // GetPageElements();
    
    // Clear the user password.
    password.value = "";

    // On success there will be a forms 
    // authentication cookie in the browser.
    if (validCredentials == true) 
    {

        // Clear the user name.
        username.value = "";

        // Hide login fields.
        buttonLogin.style.visibility = "hidden";
        usernameEntry.style.visibility = "hidden";
        passwordEntry.style.visibility = "hidden";
        textNotLoggedIn.style.visibility = "hidden";  

        // Display logout fields.
        buttonLogout.style.visibility = "visible";
        textLoggedIn.style.visibility = "visible";

        // Clear the feedback area.
        DisplayInformation(""); 
    }
    else 
    {
        textLoggedIn.style.visibility = "hidden";
        textNotLoggedIn.style.visibility = "visible";
        DisplayInformation(
            "Login Credentials Invalid. Could not login"); 
    }
    
}

// This is the callback function called 
// if the user logged out successfully.
function OnLogoutCompleted(result) 
{
    // Display login fields.
    usernameEntry.style.visibility = "visible";
    passwordEntry.style.visibility = "visible";
    textNotLoggedIn.style.visibility = "visible";  
    buttonLogin.style.visibility = "visible";

    // Hide logout fields.
    buttonLogout.style.visibility = "hidden";
    textLoggedIn.style.visibility = "hidden";

}            

// This function displays feedback
// information for the user.    
function DisplayInformation(text)
{
    // var feedBack = 
    //    document.getElementById("FeedBackID").innerHTML;

    document.getElementById("FeedBackID").innerHTML = 
        "<br/>" + text;

    // Display authentication service information.

    var userLoggedIn =
        Sys.Services.AuthenticationService.get_isLoggedIn();
    
    var authServiceTimeout =       
        Sys.Services.AuthenticationService.get_timeout();

    var userLoggedInfo = 
        "<br/> User logged in:                 " + userLoggedIn;

    var timeOutInfo = 
        "<br/> Authentication service timeout: " + authServiceTimeout;

    document.getElementById("FeedBackID").innerHTML = 
        userLoggedInfo + timeOutInfo;

}

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

See Also

Concepts

Sys.Services.ProfileService Class

Sys.Net.WebServiceProxy Class

Using Forms Authentication with Microsoft Ajax