using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Configuration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Claims;
using Microsoft.Office.SecureStoreService.Server;
using Microsoft.BusinessData.Infrastructure.SecureStore;
namespace Laxmikant
{
public static class SecureStoreManagement
{
internal static SecureString MakeSecureString(string str)
{
if (str == null)
{
return null;
}
SecureString str2 = new SecureString();
char[] chArray = str.ToCharArray();
for (int i = 0; i < chArray.Length; i++)
{
str2.AppendChar(chArray[i]);
chArray[i] = '0';
}
return str2;
}
internal static SecureStoreServiceApplicationProxy GetSecureStoreProxy()
{
string siteurl = ConfigurationSettings.AppSettings["SPSiteUrl"];//from app.config
using (SPSite site = new SPSite(siteurl))
{
SPServiceContext context =
SPServiceContext.GetContext(site);//SPServiceApplicationProxyGroup.Default, SPSiteSubscriptionIdentifier.Default);
SecureStoreServiceApplicationProxy sssProxy = context.
GetDefaultProxy(typeof(SecureStoreServiceApplicationProxy)) as SecureStoreServiceApplicationProxy;
return sssProxy;
}
}
public static void SetCredentials(string userName, string userPassword, string targetApplicationID)
{
targetApplicationID = ConfigurationSettings.AppSettings["Name"]; //from app.config
userName="SET USER NAME";
userPassword="PASSWORD";
IList
creds.Add(new SecureStoreCredential(MakeSecureString(userName), SecureStoreCredentialType.WindowsUserName));
creds.Add(new SecureStoreCredential(MakeSecureString(userPassword), SecureStoreCredentialType.WindowsPassword));
using (SecureStoreCredentialCollection credentials = new SecureStoreCredentialCollection(creds))
{
SecureStoreServiceApplicationProxy sssProxy = GetSecureStoreProxy();
if (sssProxy != null)
{
SPClaim claim = SPClaimProviderManager.CreateUserClaim("farm admin username", SPOriginalIssuerType.Windows);
SecureStoreServiceClaim ssClaim = new SecureStoreServiceClaim(claim);
sssProxy.SetUserCredentials(targetApplicationID, ssClaim, credentials);
//if the target application is using group type credentials then call this.//sssProxy.SetGroupCredentials(targetApplicationID, credentials);
}
}
}
}
}
No comments:
Post a Comment