在MOSS2010中使用SSO
在MOSS2007中,微软提供了SSO服务,但在2010中您可能没那么容易的找到相应的东西。实际上,在2010里,SSO已经被改为SSS(Secure Store Service).
下边是2010中的Get/Set credentials的实现: (具体的配置google一下)
代码
private SecureString toSecureString(string sourceString)
{
SecureString secure = new SecureString();
foreach (char c in sourceString.ToCharArray())
{
secure.AppendChar(c);
}
return secure;
}
public override void SetCredentials(string[] userInfo)
{
try
{
List<SecureStoreCredential> creds = new List<SecureStoreCredential>();
SecureStoreCredential name = new SecureStoreCredential(toSecureString(userInfo[0]), SecureStoreCredentialType.UserName);
SecureStoreCredential pwd = new SecureStoreCredential(toSecureString(userInfo[1]), SecureStoreCredentialType.Password);
SecureStoreCredential dm = new SecureStoreCredential(toSecureString(userInfo[2]), SecureStoreCredentialType.Generic);
SecureStoreCredential reps = new SecureStoreCredential(toSecureString(userInfo[3]), SecureStoreCredentialType.Generic);
creds.Add(name);
creds.Add(pwd);
creds.Add(dm);
creds.Add(reps);
SecureStoreCredentialCollection credes = new SecureStoreCredentialCollection(creds.ToArray());
SecureStoreServiceProxy proxySs = new SecureStoreServiceProxy();
SPServiceContext context = SPServiceContext.GetContext(HttpContext.Current);
ISecureStore store = proxySs.GetSecureStore(context);
store.SetCredentials(ApplicationName, credes);
}
catch (Exception ex)
{
LogUtility.Instance.Error("SSOMOSS2010-SetCredentials failed. " + ex.ToString());
throw;
}
}
public override string[] GetCredentials()
{
string[] nullArr = new string[4];
try
{
string[] strRet = new string[4];
SPServiceContext context = SPServiceContext.GetContext(HttpContext.Current);
SecureStoreProvider providerSs = new SecureStoreProvider();
providerSs.Context = context;
SecureStoreCredentialCollection creds = providerSs.GetCredentials(ApplicationName);
if (creds.Count != strRet.Length)
{
LogUtility.Instance.Error("The moss sso configuration is incorrect.");
return nullArr;
}
for (int i = 0; i < creds.Count; i++)
{
IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(creds[i].Credential);
string sDecrypString = System.Runtime.InteropServices.Marshal.PtrToStringUni(ptr);
strRet[i] = sDecrypString;
}
return strRet;
}
catch (Exception ex)
{
LogUtility.Instance.Error("SSOMOSS2010-GetCredentials failed. " + ex.ToString());
return nullArr;
}
}
}