namespace
SecureStoreCredentialsExample
{
public
static
class
SecureStoreProxy
}
using
System;
System.Linq;
System.Runtime.InteropServices;
System.Security;
Microsoft.BusinessData.Infrastructure.SecureStore;
Microsoft.Office.SecureStoreService.Server;
Microsoft.SharePoint.Administration;
Microsoft.SharePoint;
enum
CredentialType
Domain,
Generic
UserCredentials : IDisposable
private
readonly
SecureString _userName;
String UserName
get
return
ConvertToUnsecuredString(_userName); }
String DomainName;
SecureString _password;
String Password
ConvertToUnsecuredString(_password); }
UserCredentials(SecureString username, SecureString password)
_userName = username.Copy();
_password = password.Copy();
UserCredentials(SecureString username, SecureString password, SecureString domain)
DomainName = ConvertToUnsecuredString(domain);
string
ConvertToUnsecuredString(SecureString securedString)
if
(securedString ==
null
)
String.Empty;
IntPtr uString = IntPtr.Zero;
try
uString = Marshal.SecureStringToGlobalAllocUnicode(securedString);
Marshal.PtrToStringUni(uString);
finally
Marshal.ZeroFreeGlobalAllocUnicode(uString);
Boolean _isDisposed;
void
Dispose()
(_isDisposed)
;
_userName.Dispose();
_password.Dispose();
_isDisposed =
true
SPSite GetCentralAdministrationSite()
var webApplication = SPAdministrationWebApplication.Local;
(webApplication ==
throw
new
NullReferenceException(
"Unable to get the Central Administration Site."
);
var caWebUrl = webApplication.GetResponseUri(SPUrlZone.Default);
(caWebUrl ==
"Unable to get the Central Administration Site. Could get the URL of the Default Zone."
webApplication.Sites[caWebUrl.AbsoluteUri];
UserCredentials GetCredentialsFromSecureStoreService(
applicationId, CredentialType credentialType)
ISecureStoreProvider provider = SecureStoreProviderFactory.Create();
(provider ==
InvalidOperationException(
"Unable to get an ISecureStoreProvider"
var providerContext = provider
as
ISecureStoreServiceContext;
(providerContext ==
"Failed to get the provider context as ISecureStoreServiceContext"
providerContext.Context = SPServiceContext.GetContext(GetCentralAdministrationSite());
(SecureStoreCredentialCollection credentials = provider.GetCredentials(applicationId))
var un = from c
in
credentials
where c.CredentialType == (credentialType == CredentialType.Domain ? SecureStoreCredentialType.WindowsUserName : SecureStoreCredentialType.UserName)
select c.Credential;
var pd = from c
where c.CredentialType == (credentialType == CredentialType.Domain ? SecureStoreCredentialType.WindowsPassword : SecureStoreCredentialType.Password)
var dm = from c
where c.CredentialType == SecureStoreCredentialType.Key
SecureString userName = un.First(d => d.Length > 0);
SecureString password = pd.First(d => d.Length > 0);
SecureString domain = dm.First(d => d.Length > 0);
var userCredientals =
UserCredentials(userName, password, domain);
userCredientals;
System.Collections.Generic;
System.ComponentModel;
System.Web;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
System.DirectoryServices.AccountManagement;
System.Text;
IdentityType = System.DirectoryServices.AccountManagement.IdentityType;
SecureStoreCredentialsExample.GetGroupMembership
[ToolboxItemAttribute(
false
)]
GetGroupMembership : WebPart
Label _results;
TextBox _groupName;
Button _submit;
protected
override
OnInit(EventArgs e)
base
.OnInit(e);
_results =
Label();
_groupName =
TextBox();
_submit =
Button {Text =
"Submit"
};
_submit.Click += SubmitOnClick;
CreateChildControls()
Controls.Add(_groupName);
Controls.Add(_submit);
Controls.Add(
LiteralControl(
"<br/>"
));
Controls.Add(_results);
SubmitOnClick(
object
sender, EventArgs eventArgs)
StringBuilder output =
StringBuilder();
GroupPrincipal group = GetGroup(_groupName.Text);
(group ==
_results.Text =
"Group not found."
output.Append(String.Format(
"Current user, {0}, {1} a member of {2}"
, SPContext.Current.Web.CurrentUser.Name, IsUserMemberOfGroup(group, SPContext.Current.Web.CurrentUser.Sid, IdentityType.Sid) ?
"is"
:
"is not"
, group.DisplayName));
output.Append(
var groupMembers = GetAllUsersInGroup(group,
String members = String.Empty;
foreach
(UserPrincipal userPrincipal
groupMembers)
members = String.Format(
"{0}{1}"
, String.IsNullOrEmpty(members) ?
""
: String.Format(
"{0}, "
, members), userPrincipal.DisplayName);
"The current list of users in the {0} group are: {1}"
, group.DisplayName, members));
_results.Text = output.ToString();
catch
(Exception e)
_results.Text = e.Message;
UserPrincipal GetUser(String identity, IdentityType identityType)
PrincipalContext principalContext = GetPrincipalContext;
UserPrincipal.FindByIdentity(principalContext, identityType, identity);
IEnumerable<UserPrincipal> GetAllUsersInGroup(GroupPrincipal groupPrincipal, Boolean recurse)
PrincipalSearchResult<Principal> members = groupPrincipal.GetMembers(recurse);
members.OfType<UserPrincipal>().ToList();
GroupPrincipal GetGroup(String groupName)
GroupPrincipal.FindByIdentity(principalContext, IdentityType.Name, groupName);
Boolean IsUserMemberOfGroup(GroupPrincipal groupPrincipal, String identity, IdentityType identityType)
UserPrincipal userPrincipal = GetUser(identity, identityType);
(userPrincipal ==
userPrincipal.IsMemberOf(groupPrincipal);
PrincipalContext GetPrincipalContext
(var userCredientals = SecureStoreProxy.GetCredentialsFromSecureStoreService(
"ActiveDirectoryConnection"
, SecureStoreProxy.CredentialType.Domain))
var principalContext =
PrincipalContext(ContextType.Domain, userCredientals.DomainName, userCredientals.UserName, userCredientals.Password);
principalContext;