AdomdConnection可以接受令牌吗

时间:2019-10-30 12:49:22

标签: c# azure azure-analysis-services

我有以下代码可以使用C#访问SQL,分析服务是否等效?我尝试了不同的方法,但看不到可以在哪里提供访问令牌?

创建SQL连接

return new SqlConnection($"Data Source=abc; Initial Catalog=def")
{
    AccessToken = GetToken()
};

Analysis Services连接

var builder = new OleDbConnectionStringBuilder
{
    ConnectionString = "abc"
};

builder.Add("Password", "5r6utviub");

return new AdomdConnection(builder.ConnectionString);

GetToken方法在下面

        var request = (HttpWebRequest)WebRequest.Create("xxx");
        request.Headers["Metadata"] = "true";
        request.Method = "GET";
        string accessToken;
        try
        {
            var response = (HttpWebResponse)request.GetResponse();
            var streamResponse = new StreamReader(response.GetResponseStream());
            string stringResponse = streamResponse.ReadToEnd();
            JavaScriptSerializer j = new JavaScriptSerializer();
            var list = (Dictionary<string, string>)j.Deserialize(stringResponse, typeof(Dictionary<string, string>));
            accessToken = list["access_token"];
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        return accessToken;

更新

var builder = new OleDbConnectionStringBuilder
{
    ConnectionString = $"Provider=MSOLAP;Data Source=asazure://somewhere.asazure.windows.net/xyz;Catalog=mydb;Password={GetToken()};Persist Security Info=True;Impersonation Level=Impersonate"
};
return new AdomdConnection(builder.ConnectionString);

conn.Open();  // fails with Authentication failed

更新#2-显示连接字符串

Provider=MSOLAP;
Data Source=asazure://somewhere.asazure.windows.net/xyz;
Persist Security Info=True;
Password=qwertyuytrxtcfyvgubhkvjchxye56udb4sxcbhvutycxt;
Impersonation Level=Impersonate;
catalog=db

2 个答案:

答案 0 :(得分:0)

要在AdomdConnection中使用承载令牌,请按以下格式设置连接字符串:

 var server = ...;
 var token = ...;
 var constr = $"Data Source={server};Password={token};Catalog={database};Persist Security Info=True; Impersonation Level=Impersonate";

与OleDbConnection相比,AdomdConnection与不同ADOMD.NET是AS / AAS的本地.NET库,您应该使用它。

答案 1 :(得分:0)

是的完整解决方案是:

        private static string GetAccessToken()
    {
        string clientId = "xxx";
        string aadTenantId = "xxx";
        string clientSecretKey = "xxx";

        string AadInstance = "https://login.windows.net/{0}";
        string ResourceId = "https://northeurope.asazure.windows.net/"; //Be careful it must be your resource location. please find it on your azure service. Otherwise you can take token but you cannot login your service.

        AuthenticationContext authenticationContext = new AuthenticationContext(string.Format(AadInstance, aadTenantId));
        ClientCredential clientCredential = new ClientCredential(clientId, clientSecretKey);

        AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(ResourceId, clientCredential).Result;

        return authenticationResult.AccessToken;
    }

并使用它:

        static void Main(string[] args)
    {            
        var accessToken = GetAccessToken();

        var server = "asazure://northeurope.asazure.windows.net/yourInstanceName";
        var databaseName = "MY DB NAME";

        string ConnectionString = $"Provider=MSOLAP;Data Source={server};User ID=;Password={accessToken};Catalog={databaseName};Persist Security Info=True; Impersonation Level=Impersonate";

        using (AdomdConnection adomdConnection = new AdomdConnection())
        {
            adomdConnection.ConnectionString = ConnectionString;
            AdomdCommand adomdCommand = new AdomdCommand();
            adomdCommand.Connection = adomdConnection;                
            adomdCommand.CommandText = "SAMPLE QUERY";
            adomdConnection.Open();
            CellSet cellSet = adomdCommand.ExecuteCellSet();
            adomdConnection.Close();
        }

    }