新瓶旧酒ASP.NET AJAX(10) - 客户端脚本编程(Sys.Services命名空间下的类)

简介:
[索引页]
[源码下载]


新瓶旧酒ASP.NET AJAX(10) - 客户端脚本编程(Sys.Services命名空间下的类)


作者: webabcd


介绍
ASP.NET AJAX的Sys.Services.AuthenticationService类、Sys.Services.ProfileService类、Sys.Services.ProfileGroup类完美地和ASP.NET 2.0的Membership和Profile进行了集成


关键
1、Sys.Services.AuthenticationService类的login()方法
    Sys.Services.AuthenticationService.login(userName, password, isPersistent, customInfo, redirectUrl, loginCompletedCallback, failedCallback, userContext);
    ·userName - 用户名
    ·password - 密码
    ·isPersistent - 是否跨浏览器保存认证信息(持久保留)
    ·customInfo - 保留字段,可能在将来使用
    ·redirectUrl - 登录成功后重定向到的URL
    ·loginCompletedCallback - 登录成功后的回调函数
    ·failedCallback - 登录失败后的回调函数
    ·userContext - 用户上下文
    WebService中与其对应的方法 - public bool Login()

2、登录成功后的回调函数
    function LoginComplete(bool validCredentials, userContext, methodName)
    ·validCredentials - 是否成功通过了验证
    ·userContext - 用户上下文
    ·methodName - 调用的方法名

3、Sys.Services.AuthenticationService类的logout()方法
    Sys.Services.AuthenticationService.logout(redirectUrl, logoutCompletedCallback, failedCallback, userContext);
    ·redirectUrl - 注销成功后重定向到的URL
    ·logoutCompletedCallback - 注销成功后的回调函数
    ·failedCallback - 注销失败后的回调函数
    ·userContext - 用户上下文
    WebService中与其对应的方法 - public void Logout()

4、注销成功后的回调函数
    function LogoutComplete(result, userContext, methodName)
    ·result - 保留字段,可能在将来使用,始终为null
    ·userContext - 用户上下文
    ·methodName - 调用的方法名

5、Sys.Services.AuthenticationService类的属性
    ·defaultLoginCompletedCallback - 登录成功后的回调函数
    ·defaultLogoutCompletedCallback - 注销成功后的回调函数
    ·defaultFailedCallback - 登录或注销失败后的回调函数
    ·isLoggedIn - 当前用户是否已经登录
    ·path - authentication service路径
    ·timeout - 超时时间

6、Sys.Services.ProfileService类的load()方法
    Sys.Services.ProfileService.load(propertyNames, loadCompletedCallback, failedCallback, userContext);
    ·propertyNames - Profile属性名称数组
    ·loadCompletedCallback - 成功后的回调函数
    ·failedCallback - 失败后的回调函数
    ·userContext - 用户上下文
    WebService中与其对应的方法 - public IDictionary<string, object> GetAllPropertiesForCurrentUser()和public IDictionary<string, object> GetPropertiesForCurrentUser(string[] properties)

7、读取Profile成功后的回调函数
    function onLoadCompleted(numProperties, userContext, methodName)
    ·numProperties - 返回的Profile属性个数
    ·userContext - 用户上下文
    ·methodName - 调用的方法名

8、Sys.Services.ProfileService类的save()方法
    Sys.Services.ProfileService.load(propertyNames, saveCompletedCallback, failedCallback, userContext);
    ·propertyNames - Profile属性名称数组
    ·saveCompletedCallback - 成功后的回调函数
    ·failedCallback - 失败后的回调函数
    ·userContext - 用户上下文
    ·WebService中与其对应的方法 - public int SetPropertiesForCurrentUser(IDictionary<string, object> values)

9、保存Profile成功后的回调函数
    onSaveCompleted(numProperties, userContext, methodName)
    ·numProperties - 保存的Profile属性个数
    ·userContext - 用户上下文
    ·methodName - 调用的方法名

10、Sys.Services.ProfileService类的属性
    ·读取或设置某个Profile的属性要这么写 - Sys.Services.ProfileService.properties.FieldName; 
    ·defaultLoadCompletedCallback - 读取Profile成功后的回调函数
    ·defaultSaveCompletedCallback - 保存Profile成功后的回调函数
    ·defaultFailedCallback - 读取或保存Profile失败后的回调函数
    ·path - profile service路径
    ·timeout - 超时时间

11、Sys.Services.ProfileGroup类
    ·定义一个Profile组(同时需要在Web.config中定义)

12、登录或注销失败、读取或保存Profile失败后的回调函数
    function onFailed(error, userContext, methodName)
    ·error - Sys.Net.WebServiceError对象
    ·userContext - 用户上下文
    ·methodName - 调用的方法名

13、如果要 自动调用相关服务的话,则需要在web.config中的<configuration />节点下增加类似如下的配置
<webServices> 
        <authenticationService enabled="true" requireSSL="false"/> 
        <profileService enabled="true"    
                readAccessProperties="Age, Salary" 
                writeAccessProperties="Age, Salary"    
        /> 
</webServices>
 
示例
AuthenticationService.asmx
InBlock.gif<%@ WebService Language= "C#" Class= "AuthenticationService" %> 
InBlock.gif 
InBlock.gif using System; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Services; 
InBlock.gif using System.Web.Services.Protocols; 
InBlock.gif using System.Web.Script.Services; 
InBlock.gif 
InBlock.gif[WebService(Namespace =  "http://tempuri.org/")] 
InBlock.gif[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
InBlock.gif[ScriptService] 
InBlock.gifpublic class AuthenticationService : System.Web.Services.WebService 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 登录 
InBlock.gif        /// </summary> 
InBlock.gif        /// <param name="userName">用户名</param> 
InBlock.gif        /// <param name="password">密码</param> 
InBlock.gif        /// <param name="createPersistentCookie">是否跨浏览器保存认证信息(持久保留)</param> 
InBlock.gif        /// <returns></returns> 
InBlock.gif        [WebMethod] 
InBlock.gif        public bool Login(string userName, string password, bool createPersistentCookie) 
InBlock.gif        { 
InBlock.gif                //Place code here. 
InBlock.gif 
InBlock.gif                System.Web.Security.FormsAuthentication.SetAuthCookie(userName, createPersistentCookie); 
InBlock.gif                return true
InBlock.gif        } 
InBlock.gif 
InBlock.gif        /// <summary> 
InBlock.gif        /// 注销 
InBlock.gif        /// </summary> 
InBlock.gif        [WebMethod] 
InBlock.gif        public void Logout() 
InBlock.gif        { 
InBlock.gif                //Place code here. 
InBlock.gif        } 
InBlock.gif}
 
ProfileService.asmx
InBlock.gif<%@ WebService Language= "C#" Class= "ProfileService" %> 
InBlock.gif 
InBlock.gif using System; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Services; 
InBlock.gif using System.Web.Services.Protocols; 
InBlock.gif using System.Web.Script.Services; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif 
InBlock.gif[WebService(Namespace =  "http://tempuri.org/")] 
InBlock.gif[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
InBlock.gif[ScriptService] 
InBlock.gifpublic class ProfileService : System.Web.Services.WebService 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 获取用户的全部Profile 
InBlock.gif        /// </summary> 
InBlock.gif        /// <returns></returns> 
InBlock.gif        [WebMethod] 
InBlock.gif        public IDictionary<stringobject> GetAllPropertiesForCurrentUser() 
InBlock.gif        { 
InBlock.gif                Dictionary<stringobject> dic = new Dictionary<stringobject>(); 
InBlock.gif 
InBlock.gif                dic.Add("Age", 27); 
InBlock.gif                dic.Add("Salary", 100); 
InBlock.gif 
InBlock.gif                Article article = new Article(); 
InBlock.gif                article.Title = "Article Title From Server"
InBlock.gif                article.PublishTime = DateTime.Now; 
InBlock.gif 
InBlock.gif                dic.Add("Article", article); 
InBlock.gif 
InBlock.gif                return dic; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        /// <summary> 
InBlock.gif        /// 获取用户的指定的Profile 
InBlock.gif        /// </summary> 
InBlock.gif        /// <param name="properties">属性名称数组</param> 
InBlock.gif        /// <returns></returns> 
InBlock.gif        [WebMethod] 
InBlock.gif        public IDictionary<stringobject> GetPropertiesForCurrentUser(string[] properties) 
InBlock.gif        { 
InBlock.gif                //Place code here. 
InBlock.gif                return null
InBlock.gif        } 
InBlock.gif 
InBlock.gif        /// <summary> 
InBlock.gif        /// 保存用户的Profile 
InBlock.gif        /// </summary> 
InBlock.gif        /// <param name="values">用户的Profile的字典数据</param> 
InBlock.gif        /// <returns></returns> 
InBlock.gif        [WebMethod] 
InBlock.gif        public int SetPropertiesForCurrentUser(IDictionary<stringobject> values) 
InBlock.gif        { 
InBlock.gif                //Place code here. 
InBlock.gif                return values.Count; 
InBlock.gif        } 
InBlock.gif
InBlock.gif 
/// <summary> 
/// Article实体类 
/// </summary> 
/// <remarks> 
/// 示例而已,实际项目中要继承自System.Web.Profile.ProfileBase 
/// </remarks> 

InBlock.gifpublic class Article 
InBlock.gif
InBlock.gif        private string _title; 
InBlock.gif        /// <summary> 
InBlock.gif        /// 文章标题 
InBlock.gif        /// </summary> 
InBlock.gif        public string Title 
InBlock.gif        { 
InBlock.gif                get { return _title; } 
InBlock.gif                set { _title = value; } 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        private DateTime _publishTime; 
InBlock.gif        /// <summary> 
InBlock.gif        /// 文章发布日期 
InBlock.gif        /// </summary> 
InBlock.gif        public DateTime PublishTime 
InBlock.gif        { 
InBlock.gif                get { return _publishTime; } 
InBlock.gif                set { _publishTime = value; } 
InBlock.gif        } 
InBlock.gif}
 
Sample.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Sample.aspx.cs" 
        Inherits="ClientScripting_SysServices_Sample" Title="Sys.Services命名空间下的类" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
        <div> 
                帐号:<input type="text" id="txtUserId" /></div> 
        <div> 
                密码:<input type="password" id="txtPassword" /></div> 
        <div> 
                 </div> 
        <div> 
                <input type="button" id="btnLogin" value="登录" /> 
                <input type="button" id="btnLogout" value="注销" /></div> 
        <div> 
                 </div> 
        <div> 
                年龄:<input type="text" id="txtAge" /></div> 
        <div> 
                薪水:<input type="text" id="txtSalary" /></div> 
        <div> 
                <input type="button" id="btnSave" value="保存Profile" /> 
                <input type="button" id="btnLoad" value="读取Profile" /> 
        </div> 
        <div> 
                 </div> 
        <div id="result" /> 

        <script type="text/javascript"> 
                var userId; 
                var password; 
                var login; 
                var logout; 
                var age; 
                var salary 

                function pageLoad() 
                { 
                        userId = $get("txtUserId"); 
                        password = $get("txtPassword"); 
                        login = $get("btnLogin"); 
                        logout = $get("btnLogout"); 
                        age = $get("txtAge"); 
                        salary = $get("txtSalary"); 
                         
                        // path - authentication service路径 
                        $get("result").innerHTML = "AuthenticationService path:" + Sys.Services.AuthenticationService.get_path() + "<br />"; 
                        // timeout - 超时时间 
                        $get("result").innerHTML += "AuthenticationService timeout:" + Sys.Services.AuthenticationService.get_timeout() + "<br />"; 
                        // path - profile service路径 
                        $get("result").innerHTML += "ProfileService path:" + Sys.Services.ProfileService.get_path() + "<br />"; 
                        // timeout - 超时时间 
                        $get("result").innerHTML += "ProfileService timeout:" + Sys.Services.ProfileService.get_timeout() + "<br />"; 
                }                
                                 
                function btnLogin_click() 
                { 
                        // defaultLoginCompletedCallback - 登录成功后的回调函数 
                        Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(onLoginCompleted); 
                        // defaultFailedCallback - 登录或注销失败后的回调函数 
                        Sys.Services.AuthenticationService.set_defaultFailedCallback(onFailed); 
                         
                        Sys.Services.AuthenticationService.login 
                        ( 
                                userId.value, // 用户名 
                                password.value, // 密码 
                                false, // 是否跨浏览器保存认证信息(持久保留) 
                                null, // 保留字段,可能在将来使用 
                                null, // 登录成功后重定向到的URL 
                                null, // 登录成功后的回调函数 
                                null, // 登录失败后的回调函数 
                                "用户上下文" // 用户上下文 
                        ); 
                } 
                 
                function btnLogout_click() 
                { 
                        // defaultLogoutCompletedCallback - 注销成功后的回调函数 
                        Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback(onLogoutCompleted); 
                        // defaultFailedCallback - 登录或注销失败后的回调函数 
                        Sys.Services.AuthenticationService.set_defaultFailedCallback(onFailed); 

                        Sys.Services.AuthenticationService.logout 
                        ( 
                                null, // 注销成功后重定向到的URL 
                                null, // 注销成功后的回调函数 
                                null, // 注销失败后的回调函数 
                                null // 用户上下文 
                        ); 
                } 
                 
                function onLoginCompleted(validCredentials, userContext, methodName) 
                { 
                        password.value = ""; 
                         
                        // validCredentials - 是否成功通过了验证 
                        // userContext - 用户上下文 
                        // methodName - 调用的方法名 
                        if (validCredentials)    
                        { 
                                userId.value = ""; 
                                 
                                $get("result").innerHTML = "登录成功" + "<br />"; 
                                $get("result").innerHTML += "用户上下文:" + userContext + "<br />"; 
                                $get("result").innerHTML += "调用的方法名为:" + methodName + "<br />"; 
                        } 
                        else 
                        { 
                                $get("result").innerHTML = "登录失败" + "<br />"; 
                        } 
                         
                        // isLoggedIn - 当前用户是否已经登录 
                        var status = Sys.Services.AuthenticationService.get_isLoggedIn(); 
                        $get("result").innerHTML += "登录状态:" + status + "<br />"; 
                } 
                 
                function onFailed(error, userContext, methodName) 
                {         
                        // error - Sys.Net.WebServiceError对象 
                        // userContext - 用户上下文 
                        // methodName - 调用的方法名 
                        $get("result").innerHTML += "错误信息:" + error.get_message() + "<br />"; 
                } 
                 
                function onLogoutCompleted(result, userContext, methodName)    
                { 
                        // result - 保留字段,可能在将来使用,始终为null 
                        // userContext - 用户上下文 
                        // methodName - 调用的方法名 
                        alert("成功调用" + methodName) + "<br />"; 
                } 
                 
                 
                function btnLoad_click() 
                { 
                        // defaultLoadCompletedCallback - 读取Profile成功后的回调函数 
                        Sys.Services.ProfileService.set_defaultLoadCompletedCallback(onLoadCompleted); 
                        // defaultFailedCallback - 读取或保存Profile失败后的回调函数 
                        Sys.Services.ProfileService.set_defaultFailedCallback(onFailed); 
                         
                        Sys.Services.ProfileService.load 
                        ( 
                                null, // Profile属性名称数组 
                                null, // 成功后的回调函数 
                                null, // 失败后的回调函数 
                                null // 用户上下文 
                        ); 
                } 
                 
                function btnSave_click() 
                { 
                        // defaultSaveCompletedCallback - 保存Profile成功后的回调函数 
                        Sys.Services.ProfileService.set_defaultSaveCompletedCallback(onSaveCompleted); 
                        // defaultFailedCallback - 读取或保存Profile失败后的回调函数 
                        Sys.Services.ProfileService.set_defaultFailedCallback(onFailed); 
                         
                        Sys.Services.ProfileService.properties.Age = age.value; 
                        Sys.Services.ProfileService.properties.Salary = salary.value; 
                         
                        // 定义一个Profile组(同时需要在Web.config中定义) 
                        Sys.Services.ProfileService.properties.Article = new Sys.Services.ProfileGroup(); 
                        Sys.Services.ProfileService.properties.Article.Title = "Article Title"; 
                        Sys.Services.ProfileService.properties.Article.PublishTime = new Date(); 

                        Sys.Services.ProfileService.save 
                        ( 
                                null, // Profile属性名称数组 
                                null, // 成功后的回调函数 
                                null, // 失败后的回调函数 
                                null // 用户上下文 
                        ); 
                } 
                 
                function onLoadCompleted(numProperties, userContext, methodName) 
                { 
                        // numProperties - 返回的Profile属性个数 
                        // userContext - 用户上下文 
                        // methodName - 调用的方法名 
                        $get("result").innerHTML = "通过Profile读取的属性的数量为:" + numProperties.toString() + "<br />"; 
                        $get("result").innerHTML += "Age:" + Sys.Services.ProfileService.properties.Age + "<br />"; 
                        $get("result").innerHTML += "Article Title:" + Sys.Services.ProfileService.properties.Article.Title + "<br />";         
                        $get("result").innerHTML += "Article PublishTime:" + Sys.Services.ProfileService.properties.Article.PublishTime.format("yyyy-MM-dd") + "<br />"; 
                } 

                function onSaveCompleted(numProperties, userContext, methodName) 
                { 
                        // numProperties - 保存的Profile属性个数 
                        // userContext - 用户上下文 
                        // methodName - 调用的方法名 
                        $get("result").innerHTML = "通过Profile保存的属性的数量为:" + numProperties.toString() + "<br />"; 
                } 
        </script> 

</asp:Content>
 
ScriptManager的设置
<asp:ScriptManager ID="ScriptManager1" runat="server"> 
                        <AuthenticationService Path="~/ClientScripting/SysServices/AuthenticationService.asmx" /> 
                        <ProfileService Path="~/ClientScripting/SysServices/ProfileService.asmx" /> 
                </asp:ScriptManager>
 
Web.config中的相关设置(在<system.web />节点下)
<profile enabled="true"> 
            <properties> 
                <group name="Article"> 
                    <add name="Title" type="System.String" /> 
                    <add name="PublishTime" type="System.DateTime" /> 
                </group> 
            </properties> 
        </profile>
 
 
运行结果
1、页面加载后
AuthenticationService path:/Web/ClientScripting/SysServices/AuthenticationService.asmx
AuthenticationService timeout:0
ProfileService path:ProfileService.asmx
ProfileService timeout:0

2、单击“登录”按钮
登录成功
用户上下文:用户上下文
调用的方法名为:Sys.Services.AuthenticationService.login
登录状态:true

3、单击“注销”按钮
弹出框,信息:成功调用Sys.Services.AuthenticationService.logout

4、单击“保存Profile”按钮
通过Profile保存的属性的数量为:4

5、单击“读取Profile”按钮
通过Profile读取的属性的数量为:3
Age:27
Article Title:Article Title From Server
Article PublishTime:2007-07-12


OK
[源码下载]



     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/344814 ,如需转载请自行联系原作者


相关文章
|
15小时前
|
开发框架 前端开发 JavaScript
ASP.NET AJAX使用方法概述(三)
ASP.NET AJAX使用方法概述(三)
10 1
|
15小时前
|
开发框架 缓存 前端开发
安装ASP.NET AJAX (一安装)
安装ASP.NET AJAX (一安装)
11 0
|
15小时前
|
XML 开发框架 .NET
C# .NET面试系列八:ADO.NET、XML、HTTP、AJAX、WebService
## 第二部分:ADO.NET、XML、HTTP、AJAX、WebService #### 1. .NET 和 C# 有什么区别? .NET(通用语言运行时): ```c# 定义:.NET 是一个软件开发框架,提供了一个通用的运行时环境,用于在不同的编程语言中执行代码。 作用:它为多语言支持提供了一个统一的平台,允许不同的语言共享类库和其他资源。.NET 包括 Common Language Runtime (CLR)、基础类库(BCL)和其他工具。 ``` C#(C Sharp): ```c# 定义: C# 是一种由微软设计的面向对象的编程语言,专门为.NET 平台开发而创建。 作
186 2
|
15小时前
|
SQL 开发框架 .NET
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
38 0
|
6月前
|
开发框架 前端开发 .NET
用ajax和asp.net实现智能搜索功能
用ajax和asp.net实现智能搜索功能
46 0
|
10月前
|
前端开发 JavaScript 关系型数据库
宝塔设置PHP定时任务实战记录(定时任务、ajax异步刷新API、shell脚本、访问url)
宝塔设置PHP定时任务实战记录(定时任务、ajax异步刷新API、shell脚本、访问url)
506 0
|
10月前
|
前端开发
解决.NET Core Ajax请求后台传送参数过大请求失败问题
解决.NET Core Ajax请求后台传送参数过大请求失败问题
|
11月前
|
开发框架 前端开发 JavaScript
【Asp.net】 Ajax小例子
【Asp.net】 Ajax小例子
69 0
|
15小时前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
47 0
|
15小时前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
22 0