首页 > 技术文章 > Secure Store Service应用

martin-roger 2016-05-27 13:50 原文

一、用户凭据录入

你可以使用系统默认的页面(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方法,这个方法是对字符串进行安全编码,代码是:

 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方法,而不需要知道具体的邮件服务器服务地址,代码就可以改为:

 

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

 

修改后:
        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得灵活性较好,可以存储用户的多个应用程序凭据,对于多个系统集成有很好的兼容性。有兴趣的朋友,可以一起讨论,这篇博客就先写到这里了。

推荐阅读