简体   繁体   English

AdomdConnection 可以接受令牌吗

[英]Can AdomdConnection accept a token

I have the following code to gain access to SQL using C#, is there an equivalent for analysis services?我有以下代码可以使用 C# 访问 SQL,是否有类似的分析服务? I've tried different approaches but I can't see where you can provide an access token?我尝试了不同的方法,但看不到您可以在哪里提供访问令牌?

Create SQL connection创建 SQL 连接

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

Analysis Services connection分析服务连接

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

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

return new AdomdConnection(builder.ConnectionString);

The GetToken method is below 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;

Update更新

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

Update #2 - shows the connection string更新 #2 - 显示连接字符串

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

Yes Full solution it is:是的完整解决方案是:

        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;
    }

And use it:并使用它:

        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();
        }

    }

To use a bearer token with AdomdConnection, format the connection string like this:要在 AdomdConnection 中使用不记名令牌,请按如下方式格式化连接字符串:

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

And the AdomdConnection is different than OleDbConnection. AdomdConnection 与 OleDbConnection不同 ADOMD.NET is the native .NET library for AS/AAS and it's what you should be using. ADOMD.NET是用于 AS/AAS 的本机 .NET 库,您应该使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM