如何触发本机多租户应用程序的管理员同意流程?

问题描述 投票:0回答:1

这个问题直接紧接着这个问题。 我有一个本机应用程序,它必须显示用户所属的组(更准确地说,我想显示与该组关联的 SharePoint 网站)。

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

关联 ID:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx

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

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

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

“extraQueryParameters 中重复查询参数‘提示’”

到目前为止我找到的示例和教程都是针对网络应用程序的,一旦提到这个问题,那些本来可以在 stackoverflow 上帮助我的问题却出奇地沉默了link1 link2


有没有办法触发本机应用程序的管理员同意流程?可以使用 ADAL 或其他方式来完成吗?

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

.net azure multi-tenant adal
1个回答
2
投票

您可以在不使用 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
© www.soinside.com 2019 - 2024. All rights reserved.