moss2007单点登入小实践
很久没来博客园唠叨了,今天刚好有空上来逛逛,顺便添加一篇实用的文章。很多人都知道MOSS自带了一个重要特性。方便企业多系统之间的集成和整合。而单点登入就成为了炙热可得的一把利剑(至于什么叫单点登入,新手如果不明白直接google吧)。不够要在MOSS上实现SSO,遇到的一个问题就是你得熟悉第三方系统的验证模式。在不清楚第三方系统是windows还是表单验证的却会给实现带来很大的麻烦。闲话不多说,下面直接贴图。
1:当前用户的信息在SSO数据库不存在时,跳转到SSO信息配置页面(也可以用SSO默认自带的页面,,我这就没截图了)
2:配置成功。笔者为了让大家看测试效果,让他显示在页面。
3:点击进入聊天室
下面来阐述下具体的实现思路:
1:知道要继承单点登入的第三方系统的验证方式,然后单点登入提交的方式一般用到POST 或GET 进行提交。
2:主要用到SSO 中的Credentials类的两个方法分别为:SetCredentials(配置单点登入信息)。GetCredentials(获取用户的配置信息)
实现的方式:A首先打开管理中心-操作-单点登入配置。进行SSO的相关设置。
这块的资料网上也很多,配置步骤可以直接借鉴十一的博客。
B:新建一个web应用程序
笔者在这新建两个页面:Getusermsg.ascx 和ssowebpart.ascx.添加Microsoft.Sharepoint.Portal.SingleSingon.dll ,Microsoft.Sharepoint.Portal.dll
代码如下:
Getusermsg.cs
注意:下面代码中第三方系统的url配置记得改成自己的配置。
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using Microsoft.SharePoint;
12using Microsoft.SharePoint.Portal;
13using Microsoft.SharePoint.Portal.SingleSignon;
14namespace SSOwebpart
15{
16 public partial class Getusermsg : System.Web.UI.UserControl
17 {
18 protected override void OnInit(EventArgs e)
19 {
20 base.OnInit(e);
21 SSOCanaryChecker.AddCanary(this.Page);
22
23 }
24
25 protected void Page_Load(object sender, EventArgs e)
26 {
27
28 string[] rgCredentials = null;
29 try
30 {
31
32 Credentials.GetCredentials(1, "chatroom", ref rgCredentials);
33 lb_username.Text = rgCredentials[0].ToString();
34 lb_pwd.Text = rgCredentials[1].ToString();
35 string url = "/chatroom/sso_login.asp?name=" + lb_username.Text + "&pwd=" + lb_pwd.Text+ "";
36 lb_msg.Text = "<a href="+url+" target='_blank'>进入聊天室</a>";
37
38 }
39
40 catch (SingleSignonCredsNotFoundException ssoe)
41 {
42 //在SSO数据库中找不到当前用户的信息
43 if (SSOReturnCodes.SSO_E_CREDS_NOT_FOUND == ssoe.LastErrorCode)
44
45 {
46 string strSSOLogonFormURL=SingleSignonLocator.GetCredentialEntryUrl("chatroom");
47 lb_msg.Text = "<a href=" + strSSOLogonFormURL + ">对不起!当前数据库没有您的登入信息凭证,请创建!</a>";
48 }
49
50 }
51 }
52 }
53}
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using Microsoft.SharePoint;
12using Microsoft.SharePoint.Portal;
13using Microsoft.SharePoint.Portal.SingleSignon;
14namespace SSOwebpart
15{
16 public partial class Getusermsg : System.Web.UI.UserControl
17 {
18 protected override void OnInit(EventArgs e)
19 {
20 base.OnInit(e);
21 SSOCanaryChecker.AddCanary(this.Page);
22
23 }
24
25 protected void Page_Load(object sender, EventArgs e)
26 {
27
28 string[] rgCredentials = null;
29 try
30 {
31
32 Credentials.GetCredentials(1, "chatroom", ref rgCredentials);
33 lb_username.Text = rgCredentials[0].ToString();
34 lb_pwd.Text = rgCredentials[1].ToString();
35 string url = "/chatroom/sso_login.asp?name=" + lb_username.Text + "&pwd=" + lb_pwd.Text+ "";
36 lb_msg.Text = "<a href="+url+" target='_blank'>进入聊天室</a>";
37
38 }
39
40 catch (SingleSignonCredsNotFoundException ssoe)
41 {
42 //在SSO数据库中找不到当前用户的信息
43 if (SSOReturnCodes.SSO_E_CREDS_NOT_FOUND == ssoe.LastErrorCode)
44
45 {
46 string strSSOLogonFormURL=SingleSignonLocator.GetCredentialEntryUrl("chatroom");
47 lb_msg.Text = "<a href=" + strSSOLogonFormURL + ">对不起!当前数据库没有您的登入信息凭证,请创建!</a>";
48 }
49
50 }
51 }
52 }
53}
ssowebpart.cs
1using System;
2using System.Collections;
3using System.Configuration;
4using System.Data;
5//using System.Linq;
6using System.Web;
7using System.Web.Security;
8using System.Web.UI;
9using System.Web.UI.HtmlControls;
10using System.Web.UI.WebControls;
11using System.Web.UI.WebControls.WebParts;
12using System.Xml.Linq;
13using Microsoft.SharePoint;
14using Microsoft.SharePoint.Portal;
15using Microsoft.SharePoint.Portal.SingleSignon;
16
17namespace SSOwebpart
18{
19 public partial class ssowebpart : System.Web.UI.UserControl
20 {
21 protected void Page_Load(object sender, EventArgs e)
22 {
23
24 }
25
26
27 //单点登入用户配置
28 protected void btn_save_Click(object sender, EventArgs e)
29 {
30 if (txt_username.Text == "" || txt_username.Text == null)
31 {
32 lb_msg.Text = "请输入用户名!";
33 return;
34
35 }
36 if (txt_pwd.Text == "" || txt_pwd.Text == null)
37 {
38 lb_msg.Text = "密码不为空!";
39 return;
40
41 }
42 string[] rgSetCredentialData = new string[2];
43 rgSetCredentialData[0] = this.txt_username.Text;
44 rgSetCredentialData[1] = this.txt_pwd.Text;
45 try
46 {
47 Credentials.SetCredentials(1, "chatroom", rgSetCredentialData);
48 }
49 catch (SingleSignonException esso)
50 {
51 this.lb_msg.Text = esso.Message + "<br>" + esso.StackTrace;
52 }
53 }
54 }
55}
2using System.Collections;
3using System.Configuration;
4using System.Data;
5//using System.Linq;
6using System.Web;
7using System.Web.Security;
8using System.Web.UI;
9using System.Web.UI.HtmlControls;
10using System.Web.UI.WebControls;
11using System.Web.UI.WebControls.WebParts;
12using System.Xml.Linq;
13using Microsoft.SharePoint;
14using Microsoft.SharePoint.Portal;
15using Microsoft.SharePoint.Portal.SingleSignon;
16
17namespace SSOwebpart
18{
19 public partial class ssowebpart : System.Web.UI.UserControl
20 {
21 protected void Page_Load(object sender, EventArgs e)
22 {
23
24 }
25
26
27 //单点登入用户配置
28 protected void btn_save_Click(object sender, EventArgs e)
29 {
30 if (txt_username.Text == "" || txt_username.Text == null)
31 {
32 lb_msg.Text = "请输入用户名!";
33 return;
34
35 }
36 if (txt_pwd.Text == "" || txt_pwd.Text == null)
37 {
38 lb_msg.Text = "密码不为空!";
39 return;
40
41 }
42 string[] rgSetCredentialData = new string[2];
43 rgSetCredentialData[0] = this.txt_username.Text;
44 rgSetCredentialData[1] = this.txt_pwd.Text;
45 try
46 {
47 Credentials.SetCredentials(1, "chatroom", rgSetCredentialData);
48 }
49 catch (SingleSignonException esso)
50 {
51 this.lb_msg.Text = esso.Message + "<br>" + esso.StackTrace;
52 }
53 }
54 }
55}