# Add Users and Roles in Event Script

**URL:** <https://forum.inductiveautomation.com/t/add-users-and-roles-in-event-script/10381>\
**Category:** Ignition\
**Created:** [August 22, 2015, 8:46am UTC](https://forum.inductiveautomation.com/t/add-users-and-roles-in-event-script/10381 "2015-08-22T08:46:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![adnanS](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@adnanS](https://forum.inductiveautomation.com/u/adnanS)\
**Post date:** [August 22, 2015, 8:46am UTC](https://forum.inductiveautomation.com/t/add-users-and-roles-in-event-script/10381/1 "2015-08-22T08:46:32Z")

</div>

Hi There,

I am looking to do something relatively simple. I want to add an event script that will Add a new user, Add a new role and assign roles to that new user. For example when an manager clicks a button a new user and role is added for him to manipulate in the User Management page.

Is there a way of doing this in Ignition?

Kind Regards,  
Adnan

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [August 24, 2015, 8:58am UTC](https://forum.inductiveautomation.com/t/add-users-and-roles-in-event-script/10381/2 "2015-08-24T08:58:47Z")

</div>

Hi Adnan,

You can do this if you set up your User Source with a type of Database, meaning that the user and role details are stored in a SQL database. You can then create a script to write directly to the correct tables: user details go in the auth\_users table, roles in the auth\_roles table and users are mapped to roles in the auth\_user\_rl table.

The easiest thing to do is create a User Source with a type of Database and add a dummy user before looking at the tables to see how those details are stored. Note that the user’s password is stored as an MD5 hash. Creating a user in MySQL would look something like `INSERT INTO auth_users SET username='John', passwd=MD5('password'), fname='John', lname='Smith', schedule='Always', language='en';`

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 17, 2019, 11:58pm UTC](https://forum.inductiveautomation.com/t/add-users-and-roles-in-event-script/10381/3 "2019-06-17T23:58:01Z")

</div>

Any idea how to do this with SQL Server? I’ve tried using HASHBYTES(‘MD5’, ‘password’). It generates a hash, but SELECTing it returns a mishmash of chinese/thai characters and other random symbols. Comparing it to a password hash generated by modifying a password within the gateway yields completely different formats (top password is generated from gateway):  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/9/979be7a29049c5c12b83135292a7b2029fc5c7d6.png)

The SQL HASHBYTES passwords don’t work in Ignition.

There doesn’t seem to be any info whatsoever in the Ignition user manual for how passwords are stored ☹

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 18, 2019, 12:04am UTC](https://forum.inductiveautomation.com/t/add-users-and-roles-in-event-script/10381/4 "2019-06-18T00:04:32Z")

</div>

Try `HASHBYTES('SHA2_256', 'password')` - although the character encoding might just be a problem with character encoding (on insert or retrieval) in that DB column/table.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 18, 2019, 12:08am UTC](https://forum.inductiveautomation.com/t/add-users-and-roles-in-event-script/10381/5 "2019-06-18T00:08:02Z")

</div>

Nope, using SHA2\_256 encryption didn’t work either. I’ve tried all of the others as well (MD2, MD4, MD5, SHA, SHA1, SHA2\_256, SHA2\_512)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 18, 2019, 12:11am UTC](https://forum.inductiveautomation.com/t/add-users-and-roles-in-event-script/10381/6 "2019-06-18T00:11:01Z")

</div>

Looks like the auto DB user source does a SHA-1 encode before actually submitting the values into the database, and uses UTF-8 encoding to read the incoming string, then stores the bytes as B64 encoded data - I think that’s the step you’re missing.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 18, 2019, 12:12am UTC](https://forum.inductiveautomation.com/t/add-users-and-roles-in-event-script/10381/7 "2019-06-18T00:12:18Z")

</div>

I thought it looked like B64 encoding. I’ll add that onto it and see if it works, cheers

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 18, 2019, 12:27am UTC](https://forum.inductiveautomation.com/t/add-users-and-roles-in-event-script/10381/8 "2019-06-18T00:27:43Z")

</div>

~~ Ok, so i’ve tried going around SQL Server and instead using a few online encoders to:  
 1. encode password to SHA1 ([www.sha1-online.com](http://www.sha1-online.com))  
 Test Password: 4081  
 Result hash: aab10d229a1280f85e8176ee16023d33280e7ab7  
2. encode hash result to Base64 ([www.base64encode.org](http://www.base64encode.org))  
Result hash: YWFiMTBkMjI5YTEyODBmODVlODE3NmVlMTYwMjNkMzMyODBlN2FiNw==~~

~~then pasted that into the user’s passwd field in the table, but still can’t login. Any ideas?~~

The way that I ended up doing this so that I could bulk create users is by using Excel and the following VBA, taken from here ([https://en.wikibooks.org/wiki/Visual\_Basic\_for\_Applications/String\_Hashing\_in\_VBA](https://en.wikibooks.org/wiki/Visual_Basic_for_Applications/String_Hashing_in_VBA)).  
Add code to a worksheet new Module, and then in the cell, use the function `=SHA1_B64(A1)` to encrpyt and encode your passwords to the format needed.

```auto
Sub asdas()
    SHA1 "4081", True
End Sub

Public Function SHA1_B64(sIn_R As Range)
    SHA1_B64 = SHA1(sIn_R.Value, 1)
End Function

Public Function SHA1(ByVal sIn As String, Optional bB64 As Boolean = 0) As String
    'Set a reference to mscorlib 4.0 64-bit
    
    'Test with empty string input:
    '40 Hex: da39a3ee5e6...etc
    '28 Base-64: 2jmj7l5rSw0yVb...etc
    
    Dim oT As Object, oSHA1 As Object
    Dim TextToHash() As Byte
    Dim bytes() As Byte
            
    Set oT = CreateObject("System.Text.UTF8Encoding")
    Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed")
    
    TextToHash = oT.GetBytes_4(sIn)
    bytes = oSHA1.ComputeHash_2((TextToHash))
        
    If bB64 = True Then
       SHA1 = ConvToBase64String(bytes)
    Else
       SHA1 = ConvToHexString(bytes)
    End If
            
    Set oT = Nothing
    Set oSHA1 = Nothing
    
End Function

Private Function ConvToBase64String(vIn As Variant) As Variant

    Dim oD As Object
      
    Set oD = CreateObject("MSXML2.DOMDocument")
      With oD
        .LoadXML "<root />"
        .DocumentElement.DataType = "bin.base64"
        .DocumentElement.nodeTypedValue = vIn
      End With
    ConvToBase64String = Replace(oD.DocumentElement.Text, vbLf, "")
    
    Set oD = Nothing

End Function

Private Function ConvToHexString(vIn As Variant) As Variant

    Dim oD As Object
      
    Set oD = CreateObject("MSXML2.DOMDocument")
      
      With oD
        .LoadXML "<root />"
        .DocumentElement.DataType = "bin.Hex"
        .DocumentElement.nodeTypedValue = vIn
      End With
    ConvToHexString = Replace(oD.DocumentElement.Text, vbLf, "")
    
    Set oD = Nothing

End Function

```
