[源码下载]
新瓶旧酒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 />节点下增加类似如下的配置
示例
AuthenticationService.asmx
ProfileService.asmx
Sample.aspx
ScriptManager的设置
Web.config中的相关设置(在<system.web />节点下)
运行结果
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
[源码下载]