需求:在我们的CRM2011系统中,要跳转到新的系统中,可是又不想再次输入密码,这个时候,我们就应该考虑使用单点登录了。
解决方案:这里,由于系统安全性要求不是很高,所以是先将帐号和密码加密后,直接传送给新系统,当新系统接收到帐号和密码后解密自动登录。
新建一个html页面作为中转页面Skip.htm,hxcs_Huaxu.RESTAPI.js是自定义的一个helper js类库代码。如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script language="javascript" src="/WebResources/hxcs_Huaxu.RESTAPI.js"></script>
</head>
<body>
<script type="text/javascript">
OpenHtml();
function OpenHtml() {
var userId = "";
userId = parent.Xrm.Page.context.getUserId();
var queryString = "$select=DomainName&$filter=SystemUserId eq guid'" + userId + "'";
var results = Huaxu.RESTAPI.retrieveMultipleRecordsSynchro("SystemUser", queryString);
var userName = "";
if (results.exception != null) {
alert("获取用户名失败\n" + results.exception.description);
return;
}
else {
var _domainName = results.result[0].DomainName;
userName = _domainName.substring(_domainName.indexOf("\\") + 1, _domainName.length);
}
window.location.href = "/ISV/Skip.aspx?userid=" + userName;
window.opener = null;
window.open("", "_self");
window.close();
}
</script>
</body>
</html>
新建一个aspx页面Skip.aspx,代码如下:
<%@ Page Language="C#" AutoEventWireup="true" Inherits="Skip" Codebehind="Skip.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function showAll(url) {
window.open(url, "", "channelmode,scrollbars")
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace IsWaterWeb
{
public partial class Skip : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string userid = Request.QueryString["userid"];
if (string.IsNullOrEmpty(userid))
return;
byte[] bytes = Encoding.Default.GetBytes(userid);
string encryptUserId = Convert.ToBase64String(bytes);
//http://172.16.200.226:8080/koyoo/LoginByOther.action?Name=Y2NhZG1pbg==&Pwd=Y2NhZG1pbg==
string url = string.Format("http://172.16.200.226:8080/koyoo/LoginByOther.action?Name={0}&Pwd=MDAwMDAw", encryptUserId);
//Response.Redirect(url);
ClientScript.RegisterStartupScript(this.GetType(), "show", "<script>showAll('" + url + "')</script>");
}
}
}