如何实施Tableau可信认证?

时间:2018-11-07 15:36:58

标签: javascript c# asp.net embed tableau

1)在查看Web应用程序中的嵌入式仪表板时,提示用户登录Tableau。

2)如果他们关闭浏览器,启动其他浏览器会话或让Tableau cookie过期,则会提示他们再次登录。

3)整天,尝试查看仪表板时,可能会提示您多次登录。这很快变得烦人和烦人。

Tableau提供了一种称为“可信身份验证”的解决方案,该解决方案绕过了手动登录过程。经过一周的调试和故障排除,我能够完成此任务。我在Stackoverflow上找不到任何解决方案,所以我想分享我如何实现这一目标的知识,希望对其他人有所帮助。

2 个答案:

答案 0 :(得分:3)

链接到Tableau的How Trusted Authentication Works

How Trusted Authentication Works

有关如何实施可信身份验证的高级视图

1)Tableau服务器必须具有 wgserver.trusted_hosts 文件的条目,该条目具有Web应用程序的主机名,才能使其中的任何一项正常工作。

2)传递了三个重要参数:

username          212456449
server            http://[server]
target_site       YourTargetSiteName

3)如果HTTP POST请求有效并且用户具有正确的Tableau许可证,则Tableau将创建48个唯一的字符票证,该票证仅在3分钟内有效。

4)我在Tableau赎回它之前以编程方式将48个唯一字符票证添加到嵌入式JavaScript中。

代码在我的网络应用中的工作方式

我创建了一个TrustedAuth类,其中包含两个方法:requestTicket()addTicket()requestTicket()是一种异步方法,它采用三个必需的参数(sso,服务器,站点)。 HTTP POST被触发并等待响应。如果Tableau响应为-1,则HTTP握手失败或用户无效。如果有效,则响应将是一个48个字符的加密字符串。

addTicket()是一种采用两个参数(票证,reportLink)的同步方法。此方法获取48个字符的加密票证,并将其附加到嵌入式JavaScript(reportLink)。

该Web应用程序向Tableau发送HTTP GET请求,该请求包括带有加密票证的嵌入式JavaScript(reportLink)。 Tableau Server兑换票证,创建会话,登录用户,不显示登录提示

TrustedAuth类

public class TrustedAuth
{
    public async Task<string> requestTicket(int sso, string server, string site)
    {
        try
        {
            //Assign parameters and values
            var values = new List<KeyValuePair<string, string>>();
            values.Add(new KeyValuePair<string, string>("username", sso.ToString()));
            values.Add(new KeyValuePair<string, string>("target_site", site));

            //Web Application is HTTP and Tableau is HTTPS, there are certification issues. I need to fake the certs out and return them as true.
            System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };

            //Instantiate HttpClient class
            var client = new HttpClient();

            //Encode Content
            var req = new HttpRequestMessage(HttpMethod.Post, server) { Content = new FormUrlEncodedContent(values) };

            //POST request
            var res = await client.SendAsync(req);

            //Get response value
            var responseString = await res.Content.ReadAsStringAsync();

            return responseString;

        }
        catch (Exception e)
        {
            System.IO.File.AppendAllText(@"c:\inetpub\wwwroot\WebApplication\TrustedAuthError.txt", ":::ERROR::: " + System.DateTime.Today.ToString() + ":::" + e.ToString() + Environment.NewLine);
            //Add Log4Net logging
        }

        return "-1";

    }

    public string addTicket(string ticket, string reportLink)
    {
        //Add ticket parameter with ticket value. I'm using </object> as my keyword to find and replace
        string addedTicket = reportLink.Replace("</object>", "<param name='ticket' value='" + ticket + "' /></object>");

        return addedTicket;
    }
}

仪表板控制器

public async Task<ActionResult> Dashboard(int Report_Num)
    {

        var ticket = "";
        //Get Trusted Tableau Authentication Ticket
        try
        {
            ticket = await _trustedAuth.requestTicket(b.getSSO(User.Identity.Name), ConfigurationManager.AppSettings["TrustedAuthTableauServer"], ConfigurationManager.AppSettings["TrustedAuthSiteName"]);
        }
        catch
        {
            ticket = "-1";
        }

        //Only add trusted Tableau Authentication ticket if it's valid, else kick user to default Report_Link which will make them login manually. 
        //You get a nasty error message if you pass in a '-1'
        if (!ticket.Equals("-1"))
        {
            ViewBag.Link = _trustedAuth.addTicket(ticket.ToString(), report_Completion_Status.Report_Link);
        }
        else
        {
            ViewBag.Link = report_Completion_Status.Report_Link;
        }

        var model = await this.GetFullAndPartialViewModel(Report_Num);

        return this.View(model);
    }

插入了票证参数的新嵌入式JavaScript(reportLink)

enter image description here

仪表板视图

@model WebReportingToolDAL.Models.ViewModels.ReportCategoryListModel
@{
    ViewBag.Title = "Dashboard";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

 <body>
    @Html.Raw(ViewBag.Link)
</body>

如果一切正常,您应该不再看到Tableau登录页面。

答案 1 :(得分:0)

这就是我的做法

    [NonAction]
    private static async Task<String> GetTableauStringAsync(string userForTableau)
    {

        string postData = "username="+ userForTableau;
        byte[] data = System.Text.Encoding.ASCII.GetBytes(postData);
        var myTicket = "";

        try
        {

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://myTableauServer.com/trusted");

            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = postData.Length;

            Stream outStream = req.GetRequestStream();
            outStream.Write(data, 0, data.Length);
            outStream.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader inStream = new StreamReader(res.GetResponseStream());
            string resString = inStream.ReadToEnd();
            inStream.Close();

            myTicket = resString;
        }
        catch (Exception ex)
        {
            string exceptionMessage = ex.Message;
            string innerException = ex.InnerException.Message;

            myTicket = "ERROR";
        }

        return myTicket;
    }

控制器

    [HttpGet]
    public async Task<ActionResult> Index()
    {        
        string resultText = String.Empty;

        var task = GetTableauStringAsync(subjectName);
        var result = await task;
        resultText = result;

        ViewBag.TableauTicket = resultText ?? " _";
        return View();
    }

Java脚本

@section Scripts {
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">

    $(document).ready(function () {
            var myTicket = $("#lblTableauTicket").text();
            var patientBKVal = $("#lblPatient_BK").text();
            var destination = "https://myTableauServer.com/trusted/" + myTicket + "/views/MyScorecard_15804618842350/MyScorecard?Patient_BK=" + patientBKVal + "&iframeSizedToWindow=true&:embed=y&:showAppBanner=false&:display_count=no&:showVizHome=no&:origin=viz_share_link";
            window.location.href = destination;
    });

</script>

}