Sharepoint 2013 Secure Store Service

SharePoint Secure Store Service 在实际案例中的应用

文章目录:

Secure Store Service介绍

Secure Store Service部署

Secure Store Service应用

之前有一篇博客讲到使用EMSManagedAPI操作Exchange邮箱,在实际SharePoint开发中,用户总是会要求在SharePoint中集成现有exchange邮件系统。在集成的过程中,如何读取、新建、更改邮件(任务、会议)是首先要解决的问题,接下来要解决的是如何通过用户验证,也就是我们经常讲到的单点登陆。由于owa采用表单验证的方式,且没有找到很好的SharePoint单点登陆有效方案,所以采用了SharePoint2010以后引入的SSS(Secure Store Service),07版本是SSO。下面就来介绍一下SharePoint Secure Store Service 在实际案例中的应用。

Secure Store Service介绍

 Secure Store Service 是SharePoint Server提供的运行在应用程序服务器上的授权服务。Secure Store Service将用户凭据存储于一个凭据的数据库,录入用户信息时,会使用密钥加密。用户凭据通常包含用户名和密码,也可以根据需要包含其他属性。通过获取Secure Store Service存储的凭据,可以用于访问其他外部应用。根据微软官方的描述,在SharePoint应用Secure Store Service的解决方案包括:

  • Excel Services 可使用安全存储提供对已发布工作簿中的外部数据源的访问。这可以用作将用户凭据传递给数据源(此过程通常需要配置 Kerberos 委托)的替代方式。如果您需要为数据身份验证配置无人参与服务帐户,则 Excel Services 需要安全存储。
  • Visio Services 可使用安全存储提供对已发布的数据连接图表中的外部数据源的访问。这可以用作将用户凭据传递给数据源(此过程通常需要配置 Kerberos 委托)的替代方式。如果您需要为数据身份验证配置无人参与服务帐户,则 Visio Services 需要安全存储。

  • PerformancePoint Services 可使用安全存储提供对外部数据源的访问。如果您需要为数据身份验证配置无人参与服务帐户,则 PerformancePoint Services 需要安全存储。

  • PowerPivot 需要安全存储以便对 PowerPivot 工作簿进行计划刷新。

  • Microsoft Business Connectivity Services 可使用安全存储将用户凭据映射到外部系统中的一组凭据。您可将每个用户的凭据映射到外部系统中的唯一帐户,也可以将一组经过身份验证的用户映射到单个组帐户。Business Connectivity Services 还可以使用安全存储来存储用于访问 SharePoint Online 中的本地数据源的凭据。

  • SharePoint 运行时可使用安全存储来存储与 Azure 服务进行通信所必需的凭据(如果任何用户应用程序需要 SharePoint 运行时来设置和使用 Azure 服务)。

 本文是除以上解决方案中的另外一种应用。

Secure Store Service部署

在使用Secure Store Service前,我们做一些设置。

一、首先,打开SharePoint管理中心,进入应用程序管理,点击“管理服务应用程序”

在服务应用程序列表中,找到Secure Store Service服务。该服务会在运行配置向导过程中默认配置,也可以点击新建手动配置(手动配置方法请看官方文档)。

二、点击进入Secure Store Service服务

1、首次配置,要生成新密钥

2、生成新密钥后,就可以新建目标应用程序,点击新建

3、进入新建页面后,填写相关内容

显示名称:目标应用程序显示名称

联系人email:填写管理员email,用来用户联系管理员;

目标应用程序类型:根据需要选择类型,比如“个人”用来存储个人凭据,“组”用来存储组凭据(一个用户组用一个凭据)

目标应用程序页 URL:一般选择默认页,系统会有一个默认页面(http:/<samplesite>/_layouts/SecureStoreSetCredentials.aspx?TargetAppId=<TargetApplicationID>)用来录入用户凭据。

4、点击下一步,进入配置字段页面,按照需要添加凭据需要包含的字段(注:以后不能编辑字段名称和字段类型)

在这里,我除默认的用户名和密码外,另外添加一列EmailAddress用来存储用户邮箱,类型选择一般(Genetic)。

5、继续点击下一步

添加目标应用程序管理员,点击完成,完成创建目标应用程序

至此我们已经完成了Secure Store Service的准备工作,接下来就是实际应用的实践。

Secure Store Service应用

一、用户凭据录入

你可以使用系统默认的页面(http:/<samplesite>/_layouts/SecureStoreSetCredentials.aspx?TargetAppId=<TargetApplicationID>)用于用户凭据录入,也可以使用自定义的页面创建、更新用户凭据。下面的代码用来更新(创建)当前用户的特定目标应用程序凭据:

复制代码
 public static void SetCredentials(string appId, string[] userInfo)
        {
            List<SecureStoreCredential> creds = new List<SecureStoreCredential>();
            SecureStoreCredential name = new SecureStoreCredential(toSecureString(userInfo[0]), SecureStoreCredentialType.WindowsUserName);
            SecureStoreCredential pwd = new SecureStoreCredential(toSecureString(userInfo[1]), SecureStoreCredentialType.WindowsPassword);
            SecureStoreCredential EmailAddress = new SecureStoreCredential(toSecureString(userInfo[2]), SecureStoreCredentialType.Generic);
            creds.Add(name);
            creds.Add(pwd);
            creds.Add(EmailAddress);
            SecureStoreCredentialCollection credes = new SecureStoreCredentialCollection(creds.ToArray());
            SecureStoreServiceProxy proxySs = new SecureStoreServiceProxy();
            SPSite site = null;
            SPWeb web = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
              {
                  site = SPContext.Current.Site;
                  web = SPContext.Current.Web;
              });
            site.AllowUnsafeUpdates = true;
            web.AllowUnsafeUpdates = true;
            SPServiceContext context = SPServiceContext.GetContext(site);
            ISecureStore store = proxySs.GetSecureStore(context);
            store.SetCredentials(appId, credes);
            web.AllowUnsafeUpdates = false;
            site.AllowUnsafeUpdates = false;
        }
复制代码

参数介绍:
appid:目标应用程序ID,也就是上面步骤新建的“FirstID”;

userInfo:从页面中获取的用户信息列表;

方法介绍:

1、创建字段实例(注:实例名称与实际目标应用程序字段名称没有关联,只要顺序对就可以了,当然类型要一致)

SecureStoreCredential name = new SecureStoreCredential(toSecureString(userInfo[0]), SecureStoreCredentialType.WindowsUserName);

上面这句语句是创建一个凭据字段,对应FirstID中的“Windows用户名”,此类型包含一个2个参数的构造函数(字段值,字段类型);

2、创建Secure Store Service代理,获取当前SharePoint Secure Store Service上下文环境

3、为site,web提升权限

SPSecurity.RunWithElevatedPrivileges(delegate()
              {
                  site
= SPContext.Current.Site;
                  web
= SPContext.Current.Web;
              });
4、使用ISecureStore的SetCredentials方法更新(创建)用户凭据。

5、最后,会注意到有一个toSecureString方法,这个方法是对字符串进行安全编码,代码是:

View Code
复制代码
 public static System.Security.SecureString toSecureString(string s)
        {
            System.Security.SecureString secureString = new System.Security.SecureString();

            foreach (Char character in s)
            {
                secureString.AppendChar(character);
            }

            return secureString;
        }
复制代码

利用上面的代码,就可以为用户配置目标应用程序的凭据。

二、根据当前用户获取该用户凭据信息

使用上面的方法将用户凭据录入后,下一步就是利用Secure Store Service获取用户凭据。

使用EMSManagedAPI操作Exchange邮箱所在的博客中,有一个步骤是需要用户的账号和密码。另外,上面在创建目标应用程序的过程中,多加了一列EmailAddress,这样我们就可以用EWS Managed API中AutodiscoverUrl方法,而不需要知道具体的邮件服务器服务地址,代码就可以改为:

View Code
原代码:
        service.Credentials = new WebCredentials(creds);
            service.Url = new Uri("https://服务器地址/ews/exchange.asmx");
            service.PreAuthenticate = true;
View Code
修改后:
        service.Credentials = new WebCredentials(creds);
            service.AutodiscoverUrl(EmailAddress);
            service.PreAuthenticate = true;

上面是对之前应用的一点优化,如果有兴趣,可以去看之前的博客。接下来是如何取得用户凭证的实例。

Secure Store Service不需要指定用户,会直接根据当前上下文获得当前登陆用户,下面是获取用户信息列表的方法:

复制代码
public List<string> GetUserCredentialCollection(string appId, SPServiceContext CurrentContext)//appid is the SSS' ID
        {
            List<string> credentialList = new List<string>();
            SecureStoreProvider prov = new SecureStoreProvider();
            SPServiceContext context = CurrentContext;

            prov.Context = context; //当前上下文信息,以便从上下文中找到当前登陆用户
            try
            {
                SecureStoreCredentialCollection cc = prov.GetCredentials(appId);
                for (int i = 0; i < cc.Count; i++)
                {
                    ISecureStoreCredential c = cc[i];
                    IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(c.Credential);
                    string sDecrypString = System.Runtime.InteropServices.Marshal.PtrToStringUni(ptr);
                    credentialList.Add(sDecrypString);
                }
            }
            catch
            {

            }
            return credentialList;
        }
复制代码

其实最重要的是for循环中的方法,根据目标应用程序ID,获取用户凭据集合,遍历用户凭据字段并存放到List中,之后就可以根据个人需求来利用这些信息。

到这里Secure Store Service的应用基本就结束了,总体来说Secure Store Service有利有弊,对于安全性要求很高的用户来说,可能并不是一个最佳的选择。但Secure Store Service得灵活性较好,可以存储用户的多个应用程序凭据,对于多个系统集成有很好的兼容性。有兴趣的朋友,可以一起讨论,这篇博客就先写到这里了。

猜你喜欢

转载自blog.csdn.net/huangtao2011/article/details/20044471