📄 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="登录" onclick="btnLogin_click()" />
<input type="button" id="btnLogout" value="注销" onclick="btnLogout_click()" /></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" onclick="btnSave_click()" />
<input type="button" id="btnLoad" value="读取Profile" onclick="btnLoad_click()" />
</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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -