在DNN中通过代码添加、修改、删除用户和给特定用户创建、删除角色
2010的第一篇技术博客。
代码
1 'new user account
2 Dim ui As UserInfo = New UserInfo()
3 ui.PortalID = 0
4 ui.FirstName = "Ision"
5 ui.LastName = "Wu"
6 ui.DisplayName = "Ision Wu"
7 ui.Username = "Ision"
8 ui.Email = "ision.wu@intertek.com"
9 ui.IsSuperUser = False
10 ui.Profile.PreferredLocale = "En-us"
11 ui.Profile.TimeZone = -480
12 ui.Profile.LastName = ui.LastName
13 ui.Profile.FirstName = ui.FirstName
14
15 'SetMembership
16 Dim oNewMembership As UserMembership = New UserMembership()
17 oNewMembership.Approved = True
18
19 oNewMembership.CreatedDate = System.DateTime.Now
20 oNewMembership.Email = ui.Email
21 oNewMembership.IsOnLine = False
22 oNewMembership.Username = ui.Username
23 oNewMembership.Password = "1234567"
24 'Bind membership to user
25 ui.Membership = oNewMembership
26
27 'add the user, ensure it was successful
28 If UserCreateStatus.Success = UserController.CreateUser(ui) Then
29 Response.Write("Success")
30 Else
31 Response.Write("Fail")
32
33 End If
34 '-------------------------------------------------------------------------
35
36 'update user account begin
37 Dim oNewUserInfo As UserInfo = UserController.GetUserByName(0, "Ision") ' 0 is the portal id
38 oNewUserInfo.DisplayName = "Ision Name Updated"
39 UserController.UpdateUser(0, oNewUserInfo)
40 '--------------------------------------------------------------------------
41
42 'delete user account information
43 Dim oDelUserInfo As UserInfo = UserController.GetUserByName(0, "Ision")
44 UserController.DeleteUser(oDelUserInfo, False, False)
45 '--------------------------------------------------------------------------
46
47 'create role to user ision
48 Dim oRoleUserInfo As UserInfo = UserController.GetUserByName(0, "Ision")
49 Dim oRole As RoleController = New RoleController()
50 oRole.AddUserRole(0, oRoleUserInfo.UserID, oRole.GetRoleByName(0, "RoleName").RoleID, DateTime.MaxValue) ' RoleName -- user custom definition
51 '--------------------------------------------------------------------------
52
53 'update role to user ision
54 Dim oRoleUserInfo_Del As UserInfo = UserController.GetUserByName(0, "Ision")
55 Dim oRole_Del As RoleController = New RoleController()
56 oRole_Del.DeleteUserRole(0, oRoleUserInfo_Del.UserID, oRole_Del.GetRoleByName(0, "RoleName").RoleID)
57 ''--------------------------------------------------------------------------
2 Dim ui As UserInfo = New UserInfo()
3 ui.PortalID = 0
4 ui.FirstName = "Ision"
5 ui.LastName = "Wu"
6 ui.DisplayName = "Ision Wu"
7 ui.Username = "Ision"
8 ui.Email = "ision.wu@intertek.com"
9 ui.IsSuperUser = False
10 ui.Profile.PreferredLocale = "En-us"
11 ui.Profile.TimeZone = -480
12 ui.Profile.LastName = ui.LastName
13 ui.Profile.FirstName = ui.FirstName
14
15 'SetMembership
16 Dim oNewMembership As UserMembership = New UserMembership()
17 oNewMembership.Approved = True
18
19 oNewMembership.CreatedDate = System.DateTime.Now
20 oNewMembership.Email = ui.Email
21 oNewMembership.IsOnLine = False
22 oNewMembership.Username = ui.Username
23 oNewMembership.Password = "1234567"
24 'Bind membership to user
25 ui.Membership = oNewMembership
26
27 'add the user, ensure it was successful
28 If UserCreateStatus.Success = UserController.CreateUser(ui) Then
29 Response.Write("Success")
30 Else
31 Response.Write("Fail")
32
33 End If
34 '-------------------------------------------------------------------------
35
36 'update user account begin
37 Dim oNewUserInfo As UserInfo = UserController.GetUserByName(0, "Ision") ' 0 is the portal id
38 oNewUserInfo.DisplayName = "Ision Name Updated"
39 UserController.UpdateUser(0, oNewUserInfo)
40 '--------------------------------------------------------------------------
41
42 'delete user account information
43 Dim oDelUserInfo As UserInfo = UserController.GetUserByName(0, "Ision")
44 UserController.DeleteUser(oDelUserInfo, False, False)
45 '--------------------------------------------------------------------------
46
47 'create role to user ision
48 Dim oRoleUserInfo As UserInfo = UserController.GetUserByName(0, "Ision")
49 Dim oRole As RoleController = New RoleController()
50 oRole.AddUserRole(0, oRoleUserInfo.UserID, oRole.GetRoleByName(0, "RoleName").RoleID, DateTime.MaxValue) ' RoleName -- user custom definition
51 '--------------------------------------------------------------------------
52
53 'update role to user ision
54 Dim oRoleUserInfo_Del As UserInfo = UserController.GetUserByName(0, "Ision")
55 Dim oRole_Del As RoleController = New RoleController()
56 oRole_Del.DeleteUserRole(0, oRoleUserInfo_Del.UserID, oRole_Del.GetRoleByName(0, "RoleName").RoleID)
57 ''--------------------------------------------------------------------------