# Connection to Azure SQL Server not working inside rule

**URL:** <https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077>\
**Category:** Get Help\
**Tags:** azure-sql\
**Created:** [September 4, 2017, 2:56am UTC](https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077 "2017-09-04T02:56:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![theivanaguilarc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/theivanaguilarc/32/7921_2.png) [@theivanaguilarc](https://community.auth0.com/u/theivanaguilarc)\
**Post date:** [September 4, 2017, 2:56am UTC](https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077/1 "2017-09-04T02:56:28Z")

</div>

I’ve created a rule from the “Add user roles from a SQL Server database” template.  
I’ve modified the code with my credentials and a custom query, both fully functional.  
However, I’m getting this message:

> ERROR: Script execution did not complete within 20 seconds. Are you calling the callback function?

Any idea of what’s going on? I’m also getting the following on the Webtask log:

> finished webtask request 1XXXXXXXXX with HTTP 500 in 35062ms  
> (changed the code with Xs).

The query and credentials are correct as I’m using them constantly without problems.

Thanks,  
Ivan

---

<div class="post-metadata">

**Author:** ![theivanaguilarc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/theivanaguilarc/32/7921_2.png) [@theivanaguilarc](https://community.auth0.com/u/theivanaguilarc)\
**Post date:** [September 4, 2017, 5:50am UTC](https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077/2 "2017-09-04T05:50:27Z")

</div>

Managed to solve it by creating the connection myself using mssql.

---

<div class="post-metadata">

**Author:** ![baltzar](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@baltzar](https://community.auth0.com/u/baltzar)\
**Post date:** [September 9, 2017, 2:18pm UTC](https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077/3 "2017-09-09T14:18:33Z")

</div>

How did you manage to solve this using the connection string yourself? I’m struggling with the same problem

---

<div class="post-metadata">

**Author:** ![theivanaguilarc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/theivanaguilarc/32/7921_2.png) [@theivanaguilarc](https://community.auth0.com/u/theivanaguilarc)\
**Post date:** [September 11, 2017, 2:34pm UTC](https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077/4 "2017-09-11T14:34:55Z")

</div>

Hi, sorry for the late reply, hope it’s not to late to help out. This is the code I used:

> function (user, context, callback) {
> 
> var mssql = require(‘mssql@3.3.0’);  
> var connection = mssql.connect({ user:  
> ‘’, password: ‘’, server: ‘’,  
> database: ‘’, options: { encrypt:  
> true, } }).then(function(pool) {  
> return pool.request() .query(‘YOUR  
> QUERY’); }).then(function(result) { //  
> DO WHATEVER WITH RESULT callback(null,  
> user, context); }); }

---

<div class="post-metadata">

**Author:** ![theivanaguilarc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/theivanaguilarc/32/7921_2.png) [@theivanaguilarc](https://community.auth0.com/u/theivanaguilarc)\
**Post date:** [September 11, 2017, 2:35pm UTC](https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077/5 "2017-09-11T14:35:41Z")

</div>

I can’t for the life of me get the code to look good, hope it’s clear enough

---

<div class="post-metadata">

**Author:** ![baltzar](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@baltzar](https://community.auth0.com/u/baltzar)\
**Post date:** [September 13, 2017, 1:24pm UTC](https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077/6 "2017-09-13T13:24:20Z")

</div>

Thanks a lot! I’ll try this out

---

<div class="post-metadata">

**Author:** ![baltzar](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@baltzar](https://community.auth0.com/u/baltzar)\
**Post date:** [September 13, 2017, 1:39pm UTC](https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077/7 "2017-09-13T13:39:57Z")

</div>

Awwww damn, it actually works! You’re a lifesaver!

---

<div class="post-metadata">

**Author:** ![theivanaguilarc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/theivanaguilarc/32/7921_2.png) [@theivanaguilarc](https://community.auth0.com/u/theivanaguilarc)\
**Post date:** [September 13, 2017, 1:42pm UTC](https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077/8 "2017-09-13T13:42:57Z")

</div>

@baltzar Glad I could help!

---

<div class="post-metadata">

**Author:** ![baltzar](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@baltzar](https://community.auth0.com/u/baltzar)\
**Post date:** [September 14, 2017, 12:09am UTC](https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077/9 "2017-09-14T00:09:09Z")

</div>

I’ll share my code, with an example of replacing parameters to avoid sql injection:

```
function (user, context, callback) {
  
  var mssql = require('mssql@3.3.0');
  
  var query = "SELECT ur.Name " +
                "FROM dbo.Users u " +
                "INNER JOIN dbo.UserRoleUsers urr " + 
                "ON urr.User_UserId = u.UserId " +
                "INNER JOIN dbo.UserRoles ur " +
                "ON ur.Id = urr.UserRole_Id " + 
                "WHERE u.Email = @email";
  
  var connection = mssql.connect( { 
    user: '', 
    password: '', 
    server: '', 
    database: '',
    options: {
      encrypt: true
    },
  }).then(function(pool) {
    return pool.request()
      // To avoid sql injection
      .input("email", mssql.VarChar, user.email)
      .query(query);
  }).then(function(result) {
    context.idToken"https://mydomain.com/roles"] = result;
    callback(null, user, context);
  });
}

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 2, 2018, 5:56am UTC](https://community.auth0.com/t/connection-to-azure-sql-server-not-working-inside-rule/8077/10 "2018-03-02T05:56:18Z")

</div>


