4

For a SQL Server instance, to check if a windows user is present and has any access or not one can try various ways as detailed here.

I'm looking for something similar for SQL Server Analysis Services (SSAS) server.

I went into properties of SSAS Server from right-click context menu and on Security tab I can see that there are several windows users already configured:

enter image description here

Is there any way to check from a client application (written in C#) by making some sort of test connection or does SSAS also maintains some metadata database of its own like master database in SQL Server instance (DB engine) which can be queried. I checked the Databases node in SSAS server but I don't see any default databases there:

enter image description here

In the client application I'm working upon, I've windows user name and password as input. In my client application there is a simple winform with two text boxes to take AD user name and password which need to be connected to a SSAS Server. My gut feel is that password is of no relevance here as SSAS supports only Windows integrated authentication mode. My client application would be running under an account which already has access to SSAS server I'm trying to connect.

Update: After getting help from @Vaishali, I'm able to figure out that it is possible to make a test connection to an SSAS server using ADOMD.Net.

Now, the problem here is that the connection string implicitly uses the AD account of the user with which I'm running the client application to connect to the SSAS server. I don't think it would be possible mention an windows AD account user name and password explicitly in the ADOMD.Net connection strings while using Windows Integrated authentication. Even connection strings of SQL Server don't allow mentioning the windows username and password explicitly in the connection string as mentioned here.

Update 2: I have got a lead from one of my friends that it is possible to fire some MDX query on SSAS to get user access details.

Update 3: SSAS server supports only Windows Integrated Security mode of authentication unlike SQL Server DB engine which also supports userid-password based SQL authentication. So, some form of impersonation would be required to fire MDX queries on behalf of other user for which I'm trying to check access on SSAS server through Windows Integrated Security only.

RBT
  • 24,161
  • 21
  • 159
  • 240

2 Answers2

2

Hmphh...It was quite a journey to really be able to nail it through ADOMD.Net.

Core methodology: The core philosophy is the fact that connection to SSAS server supports only Windows Integrated Security based authentication. The SQL authentication like we do for sa user in SQL Server isn't supported in SSAS.

So, the basic idea was to try to connect to the SSAS server using Windows Integrated Security based authentication and fire an MDX query in the context of the user we are trying to check. If the query gets executed successfully then the user has access. If the query execution returns an error/exception then the user doesn't have access.

Please note that just to be able to open a connection to the SSAS server is not an indicator of user-access due to reasons described here. You must fire a query to check access.

For ADOMD.Net until v12.x:

Now, we know that Windows Integrated Security based authentication always takes the user details from the user-context under which the application/process is running. You can not pass the user credentials in the connection string of ADOMD.Net connection. Here is the code I wrote to accomplish it. You need to refer to Microsoft.AnalysisServices.AdomdClient.dll in your C# project.

using Microsoft.AnalysisServices.AdomdClient;
public static int IsSsasAccessibleToUser(string ssasServerName)
{
    var hasAccess = 0;
    try
    {
        using (var adomdConnection = new AdomdConnection($"provider=olap;datasource={ssasServerName};Catalog=myDatabaseName"))
        using (var adomdCommand = new AdomdCommand())
        {
            adomdCommand.CommandText = "SELECT [CATALOG_NAME] AS [DATABASE],CUBE_CAPTION AS [CUBE/PERSPECTIVE],BASE_CUBE_NAME FROM $system.MDSchema_Cubes WHERE CUBE_SOURCE = 1";
            adomdCommand.Connection = adomdConnection;
            adomdConnection.Open();
            adomdCommand.ExecuteNonQuery();
            Log("ExecuteNonQuery call succeeded so the user has access");
            hasAccess = 1;
        }
    }
    catch (Exception ex)
    {
        Log("There was an error firing query on the database in SSAS server. so user doesn't have access");
    }
    return hasAccess;
}

Now, to leverage Windows Integrated Security based authentication we can run this code in two ways:

  1. Out-Proc Impersonation : Put this code inside a console application. Use the "Run as different user" option in the context menu when we right click the exe. Put the credentials of the user Y (let's say) so that application starts in the context of user Y for which we need to validate the access on SSAS server. ADOMD.Net will use user Y's identity while connecting using Windows Integrated Security for SSAS server. If code succeeds the user has access.
  2. In-Proc Impersonation: The other case could be that you are running the application as user X but you want to test the access of user Y. Here effectively you require in-place impersonation while running the above code. For achieving it I used a famous NuGet package "Simple Impersonation" which uses the default .Net library classes WindowsImpersonationContext and WindowsIdentity . Creator of this NuGet package had first posted a great answer here.

Observation in SQL Server Profiler: After you've impersonated user Y, you will clearly see the MDX query getting fired in the context of user Y if you capture the session as shown below:

enter image description here

Caveats and concerns:

  1. One issue that I faced while using this in-proc impersonation is that it doesn't work if the SSAS server is located on the same machine where the application code is running. This is due to the inherent behavior of native LogonUser API (using LOGON32_LOGON_NEW_CREDENTIALS LogonType) which is called during impersonation calls by the NuGete package. You can try other logon types as detailed here which suites you need.
  2. You require password of the user as well along with the domain name and user name to do impersonation.

For ADOMD.Net v13.x onwards

Then, I came across this ChangeEffectiveUser API documentation on MSDN here. But, intellisense wasn't showing this API. Then I found out this API got added in ADOMD.Net with SQL Server 2016 release. There are various ways to get the latest release:

  1. C:\Program Files\Microsoft.NET\ADOMD.NET\130\Microsoft.AnalysisServices.AdomdClient.dll I'm not sure who dumps this file at this location. Is it part of Microsoft.Net extensions or SQL Server installation.
  2. In Installation folder of Microsoft SQL Server. I got it at path - C:\Program Files\Microsoft SQL Server\130\Setup Bootstrap\Update Cache\KB3182545\ServicePack\x64\Microsoft.AnalysisServices.AdomdClient.dll
  3. NuGet package here. For some weird reason best known to MS the NuGet package of v13.x of ADOMD.Net has been named Unofficial.Microsoft.AnalysisServices.AdomdClient. Not sure why they introduced a separate NuGet package with Unofficial prefix when this should have been simply the next version of the already existing NuGet package Microsoft.AnalysisServices.AdomdClient present here.

So the new API ChangeEffectiveUser present in latest version on AdomdConnection clas can be used easily to impersonate any user as below:

adomdConnection.Open();
//impersonate the user after opening the connection
adomdConnection.ChangeEffectiveUser("domainName\UserNameBeingImpersonated");
//now the query gets fired in the context of the impersonated user
adomdCommand.ExecuteNonQuery();

Observing Impersonation in SQL Server Profiler: Although one peculiar observation I had in the SQL Server Profiler is that the logs of query being fired still shows the name of the original user with which your application process is running.

enter image description here

So to check whether impersonation is happening or not I removed the access rights of the user domainName\UserNameBeingImpersonated from SSAS server. After that, when I ran the above code again then it resulted in exception whose message clearly states that - the user domainName\UserNameBeingImpersonated doesn't have permission on the SSAS server or the database doesn't exist. This error message clearly suggests that impersonation is working.

Advantages and Backward compatibility of this approach:

  1. Although the API is very recent as it came up with SQL Server 2016 but I was able to use it successfully with SSAS server 2014 as well. So it looks fairly backward compatible.
  2. This API works irrespective of whether your SSAS server is local or remote.
  3. You just require the domain name and user name for doing impersonation. No password require.

What to do if we simply want to check the access on the SSAS server without involving any database present on the SSAS server?

  1. Change the connection string to not involve any database. Remove the Catalog key as following connection string - "provider=olap;datasource={ssasServerName};"
  2. Fire the following query instead to check access - SELECT * FROM $System.discover_locks in the code snippet shown initially in the post.
RBT
  • 24,161
  • 21
  • 159
  • 240
0

If you wish to check if user has accessibility to SSAS server, one option you can try with C# is: try connecting SSAS with given user credential, if you succeed, you have access.

If you are looking for roles and security mapped to individual cube database, following link will be usefull.

http://www.lucasnotes.com/2012/09/list-ssas-user-roles-using-powershell.html#comment-form

C# code lines:

import library Microsoft.AnalysisServices.AdomdClient; and code lines would be:

      DataSet ds = new DataSet();
          AdomdConnection myconnect = new AdomdConnection(@"provider=olap;datasource=.\SQL20f12");
          AdomdDataAdapter mycommand = new AdomdDataAdapter();
          mycommand.SelectCommand = new AdomdCommand();
          mycommand.SelectCommand.Connection = myconnect;

          try
          {
              myconnect.Open(); 
          }
          catch 
          { 
              MessageBox.Show("error in connection");
          }                    

Hope this works for you.

Vaishali
  • 41
  • 4
  • I don't have any user-defined cube, database, etc yet in my SSAS server. How do I connect to the SSAS server from C# code? Though some connection string? – RBT Jun 30 '17 at 10:00
  • for testing SSAS server connection Cube/Database is not mandatory. [Link](https://stackoverflow.com/questions/12563417/how-to-test-connection-to-a-data-source-in-ssas-using-c-sharp) – Vaishali Jun 30 '17 at 10:49
  • Can you provide or point me to some link containing the C# code which does it? – RBT Jun 30 '17 at 10:50
  • Link added in prior comment. sharing it again [How to test connection to a data source in SSAS using C#] (https://stackoverflow.com/questions/12563417/how-to-test-connection-to-a-data-source-in-ssas-using-c-sharp) – Vaishali Jun 30 '17 at 12:08
  • I had already seen that post. It uses a [deprecated Nuget package](https://www.nuget.org/packages/Microsoft.AnalysisServices/) `Microsoft.AnalysisServices` which is not being maintained any more . I came across another NuGet package [Microsoft.AnalysisServices.Server](https://www.nuget.org/packages/Microsoft.AnalysisServices.Server/) but haven't been able to get any source code on how to use it. This NuGet package has [this](https://msdn.microsoft.com/en-us/library/microsoft.analysisservices.core.server.aspx) `Server` class. – RBT Jun 30 '17 at 12:13
  • Nice. The latest code snippet works as far as checking the connection to the SSAS server is concerned. It is using the domain user with which the process is running to connect to the SSAS server. But, my problem is to check if a given user name (obtained as input in my application) has the connection access or not. It seems it is not possible to mention a domain user name explicitly in the connection string. I checked all possible ADOMD.net connection strings [here](https://www.connectionstrings.com/adomd-net/).The very same restriction is there while connecting to SQL Server instance also. – RBT Jul 01 '17 at 02:26
  • So essentially I need to be able to fire some query on SSAS server like the way I have mentioned in my post for SQL Server instance. Some folks are telling me that it is possible by firing some MDX query on SSAS server but I'm a complete newbie there. On a contrary I don't see any metadata database when I open SSAS instance (like `master` DB in SQL Server) through SSMS where I could be firing any query if at all. – RBT Jul 01 '17 at 02:31
  • I was finally able to solve it. Thanks for all the initial pointers that you gave. I've added an answer based on my entire findings. Hope it can act as good source of knowledge for the community. – RBT Jul 08 '17 at 02:45