Welcome Guest ( Login | Register )



All times are UTC - 7 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Adding user via script
PostPosted: Tue Jul 17, 2012 12:21 am 

Joined: Mon Sep 12, 2011 4:52 am
Posts: 29

Offline
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!


 Profile  
 
 Post subject: Re: Adding user via script
PostPosted: Tue Jul 17, 2012 7:23 am 

Joined: Tue Mar 13, 2012 9:53 am
Posts: 824

Offline
Take a look at the attached project. It uses a script to add users to the system.

Code:
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 673 times

_________________
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer
 Profile  
 
 Post subject: Re: Adding user via script
PostPosted: Wed Jul 18, 2012 12:27 am 

Joined: Mon Sep 12, 2011 4:52 am
Posts: 29

Offline
Thank you very much!
Best regards


 Profile  
 
 Post subject: Re: Adding user via script
PostPosted: Thu Apr 30, 2015 2:07 am 

Joined: Fri Jan 27, 2012 4:20 am
Posts: 161

Offline
Works fine. But, how can be user deleted via script?


 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 7 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: