Sys.Services.AuthenticationService 类

更新:2007 年 11 月

为身份验证服务提供客户端代理类。

命名空间:Sys.Services

**继承:**无

成员

名称

说明

Sys.Services.AuthenticationService 构造函数

初始化 Sys.Services.AuthenticationService 类的新实例。

Sys.Services.AuthenticationService.DefaultWebServicePath 字段

指定默认身份验证服务的路径。

Sys.Services AuthenticationService login 方法

对用户凭据进行身份验证。

Sys.Services AuthenticationService logout 方法

注销当前经过身份验证的用户。

Sys.Services AuthenticationService defaultFailedCallback 属性

获取或设置默认失败回调函数的名称。

Sys.Services AuthenticationService defaultLoginCompletedCallback 属性

获取或设置默认登录完成回调函数的名称。

Sys.Services AuthenticationService defaultLogoutCompletedCallback 属性

获取或设置默认注销完成回调函数的名称。

Sys.Services AuthenticationService defaultSucceededCallback 属性

获取或设置服务的默认成功回调函数。

Sys.Services AuthenticationService defaultUserContext 属性

获取或设置服务的默认用户上下文。

Sys.Services AuthenticationService isLoggedIn 属性

获取当前用户的身份验证状态。

Sys.Services AuthenticationService path 属性

获取或设置身份验证服务路径。

Sys.Services AuthenticationService timeout 属性

获取或设置身份验证服务超时。

备注

AuthenticationService 类提供对用户身份验证的脚本访问。该类调用身份验证服务方法所用的基础结构与调用任何其他 Web 服务方法所用的基础结构一样。

说明:

内置身份验证服务位于服务器上的预定义位置。

AuthenticationService 类为单一实例;它只有一个提供全局访问点的实例。它始终对应用程序可用,因此无需将其实例化。

示例

说明

下面的示例演示如何使用 AuthenticationService 类检查用户是否通过身份验证。有关更多信息,请参见将 Forms 身份验证用于 ASP.NET AJAX

代码

// 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();


请参见

概念

Sys.Services.ProfileService 类

Sys.Net.WebServiceProxy 类

将 Forms 身份验证用于 ASP.NET AJAX