【问题标题】:How can I trigger admin consent flow for a native multitenant app?如何触发原生多租户应用程序的管理员同意流程?
【发布时间】:2017-04-20 17:19:19
【问题描述】:

这个问题直接跟在this one后面。 我有一个本机应用程序,它必须显示用户所属的组(更准确地说,我想显示与该组关联的共享点站点)。

它适用于注册应用程序的租户的任何用户。但是当我尝试从另一个租户连接时,它只适用于管理员帐户。尝试连接用户帐户时出现此错误:

相关 ID:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx

时间戳:2017-04-19 08:32:24Z

AADSTS90093:由于缺少权限,调用主体无法同意。

永远不会触发管理员同意流程。正如我在上面链接的上一篇文章中所述,尝试添加“prompt=admin_consent”作为查询参数会返回此错误:

"extraQueryParameters 中的重复查询参数 'prompt'"

到目前为止,我发现的示例和教程都是针对网络应用程序的,一旦提到这个问题link1link2,本可以在 stackoverflow 上帮助我的问题都奇怪地沉默了。


是否有办法触发原生应用的管理员同意流程?可以使用 ADAL 或其他方式完成吗?

将应用程序从本机更改为网络不是一种选择,因为我只是想向现有应用程序添加新功能。
因此,如果上述问题的答案是“否”,有什么解决方法吗?我愿意接受任何建议。

【问题讨论】:

    标签: .net azure multi-tenant adal


    【解决方案1】:

    您可以在不使用 ADAL 的情况下发送管理员同意请求。由于您使用的是 WinForm 应用程序,我们可以使用 WebBrowser 控件显示登录页面,将 prompt=admin_consent 设置为请求 url 的一部分。如果管理员同意成功,请检查响应中的 admin_consent 的值是否为 True。以下代码供您参考:

        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
    
            var parameters = new Dictionary<string, string>
                {
                    { "response_type", "code" },
                    { "client_id", "<client id>" },
                    { "redirect_uri", "http://localhost" },
                    { "prompt", "admin_consent"}
                };
            var requestUrl = string.Format("{0}/authorize?{1}", "https://login.microsoftonline.com/common/oauth2", BuildQueryString(parameters));
            webBrowser1.Navigate(requestUrl);
        }
        protected string EndPointUrl
        {
            get
            {
                return string.Format("{0}/{1}", "https://login.microsoftonline.com/common", "");
            }
        }
    
        private string BuildQueryString(IDictionary<string, string> parameters)
        {
            var list = new List<string>();
    
            foreach (var parameter in parameters)
            {
                list.Add(string.Format("{0}={1}", parameter.Key, Uri.EscapeDataString(parameter.Value)));
            }
    
            return string.Join("&", list);
        }
    
        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            if (e.Url.AbsoluteUri.StartsWith("http://localhost"))
            {
                var collection = System.Web.HttpUtility.ParseQueryString(e.Url.Query);
                //check whether admin consent success 
                if (collection["admin_consent"] == "True")
                {
                    //hide/close webBrowser
                }
    
            }
        }
    

    如果您不想对代码做任何事情,租户管理员可以通过打开浏览器并转到以下 URL 手动进行管理员同意:

    https://login.microsoftonline.com/<tenant>/oauth2/authorize?client_id=<client id>&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2F&nonce=1234&resource=https%3A%2F%2Fgraph.windows.net%2F&prompt=admin_consent
    

    【讨论】:

    • 谢谢。两者似乎都工作正常。我还不知道我会选择哪种解决方案,但这个问题已经解决了。
    猜你喜欢
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 2020-09-26
    • 2019-12-24
    • 2018-06-17
    相关资源
    最近更新 更多