Good morning. I'm trying to add a user to the panel via script. I have written the following code but it doesn't work fine:
SecurityUser su =new SecurityUser();
su.Username="Username";
su.Password="Password";
SecurityGroups sg=SecurityGroups.None;
su.Groups=sg;
Globals.Security.Users.Add(su);
If I execute the code above and then I try to login (with the new user), the panel auto-restarts the project. Else, if I execute the code above, I open & close the Users Dialog (where the new user is present) and in the end I try to login, it all works.
Could you help me please?
Thank you!
Adding user via script
-
- Posts: 824
- Joined: Tue Mar 13, 2012 9:53 am
Re: Adding user via script
Take a look at the attached project. It uses a script to add users to the system.
Code: Select all
namespace Neo.ApplicationFramework.Generated
{
using System.Windows.Forms;
using System;
using System.Drawing;
using Neo.ApplicationFramework.Tools;
using Neo.ApplicationFramework.Tools.Security;
using Neo.ApplicationFramework.Common.Graphics.Logic;
using Neo.ApplicationFramework.Controls;
using Neo.ApplicationFramework.Interfaces;
using Neo.ApplicationFramework.Common.Service;
public partial class AddUsersFromTags
{
public static void AddUser(string username, string password)
{
//We will add all new users to the GROUP for lack of anything
//better to do.
const string GROUP = "Operators";
ISecurityManager securityManager = Globals.Security as ISecurityManager;
ISecurityUser newUser = new SecurityUser();
PasswordHasherCF hasher = new PasswordHasherCF();
//Create the new users here
newUser.Username = username;
newUser.Password = password;
//The ISecurityUser will not create the password hash for us, so we need to do it
newUser.PasswordHash = hasher.GetPasswordHash(password);
//Because the group is a enum, and we only know the name of the group
//we want our new user to be in, we need to cycle through the
//security groups and find out what number is associated with the
//security group we want.
foreach(ISecurityGroup aGroup in securityManager.Groups )
{
if(aGroup.GroupName == GROUP)
{
newUser.AddToGroup(aGroup.Group);
Globals.Tags.Tag1.Value = aGroup.GroupName;
break;
}
}
//All done, lets add the new user to the HMI's security system
securityManager.AddUser(newUser);
//Now save the new security file. If you do not do this, then the above changes will
//be lost on the next HMI power cycle!
ISecurityServiceCF securityServiceCF = ServiceContainerCF.GetService<ISecurityServiceCF>();
securityServiceCF.Save(securityManager.FilePath);
}
}
}
- Attachments
-
- BenSecurity_A7.zip
- (28.77 KiB) Downloaded 1464 times
Best Regards,
Mark Monroe
Beijer Electronics, Inc. | Applications Engineer
Mark Monroe
Beijer Electronics, Inc. | Applications Engineer
Re: Adding user via script
Thank you very much!
Best regards
Best regards
Re: Adding user via script
Works fine. But, how can be user deleted via script?