Code
1SPWeb site = SPContext.Current.Web;
2SPUser currentUser = site.CurrentUser;
3// create new group
4site.SiteGroups.Add("Site Members", currentUser, currentUser,
5 "Site Group created at " + DateTime.Now.ToString());
6// assign permission level to new group
7SPGroup NewGroup = site.SiteGroups["Site Members"];
8SPRoleAssignment roleAssignment = new SPRoleAssignment(NewGroup);
9SPRoleDefinition permLevel = site.RoleDefinitions["Contribute"];
10roleAssignment.RoleDefinitionBindings.Add(permLevel);
向组中添加用户:
权限提升:
模拟 WSS 用户标识:
1SPWeb site = SPContext.Current.Web;
2SPUser currentUser = site.CurrentUser;
3// create new group
4site.SiteGroups.Add("Site Members", currentUser, currentUser,
5 "Site Group created at " + DateTime.Now.ToString());
6// assign permission level to new group
7SPGroup NewGroup = site.SiteGroups["Site Members"];
8SPRoleAssignment roleAssignment = new SPRoleAssignment(NewGroup);
9SPRoleDefinition permLevel = site.RoleDefinitions["Contribute"];
10roleAssignment.RoleDefinitionBindings.Add(permLevel);
向组中添加用户:
Code
1SPWeb site = SPContext.Current.Web;
2SPUser currentUser = site.CurrentUser;
3SPGroup group = site.SiteGroups["Site Members"];
4SPUser user1 = site.SiteUsers[@"LITWAREINCBrianC"];
5SPUser user2 = site.SiteUsers[@"LITWAREINCAllFTE"];
6group.AddUser(user1);
7group.AddUser(user2);
1SPWeb site = SPContext.Current.Web;
2SPUser currentUser = site.CurrentUser;
3SPGroup group = site.SiteGroups["Site Members"];
4SPUser user1 = site.SiteUsers[@"LITWAREINCBrianC"];
5SPUser user2 = site.SiteUsers[@"LITWAREINCAllFTE"];
6group.AddUser(user1);
7group.AddUser(user2);
权限提升:
Code
1SPSecurity.RunWithElevatedPrivileges(delegate() {
2
3
4});
1SPSecurity.RunWithElevatedPrivileges(delegate() {
2
3
4});
模拟 WSS 用户标识:
Code
1SPWeb siteCollection = SPContext.Current.Site;
2SPWeb site = SPContext.Current.Web;
3// get SPUser object and acquire token
4SPUser targetUser = site.SiteUsers[@"LITWAREINCBrianC"];
5SPUserToken token = targetUser.UserToken;
6// create new SPSite and SPWeb object to impersonate user
7using (SPSite impersonatedSiteCollection =
8 new SPSite(siteCollection.ID, token)) {
9 using (SPWeb impersonatedSite =
10 impersonatedSiteCollection.OpenWeb(site.ID)) {
11 // WSS identity switched to impersonate BrianC
12 // Windows identity does not change
13 }
14}
1SPWeb siteCollection = SPContext.Current.Site;
2SPWeb site = SPContext.Current.Web;
3// get SPUser object and acquire token
4SPUser targetUser = site.SiteUsers[@"LITWAREINCBrianC"];
5SPUserToken token = targetUser.UserToken;
6// create new SPSite and SPWeb object to impersonate user
7using (SPSite impersonatedSiteCollection =
8 new SPSite(siteCollection.ID, token)) {
9 using (SPWeb impersonatedSite =
10 impersonatedSiteCollection.OpenWeb(site.ID)) {
11 // WSS identity switched to impersonate BrianC
12 // Windows identity does not change
13 }
14}