在 Azure B2C 中添加变量以回复 URL

Add variables to reply URLs in Azure B2C

我正在尝试在 Azure B2C 中设置 redirect_uri。我在 Url 中有一个语言字段,如下所示:

https://mydomain/de-de/projects
https://mydomain/en-us/projects
https://mydomain/sv-se/projects 
https://mydomain/ar-sa/projects
...

为了正确重定向,我必须将所有可能性添加到 B2C 回复 URL 中,我最多只能添加 20 个。

有没有办法向 redirect_uri 添加变量? 类似于:

https://mydomain/:lang/projects

其中“:lang”是一个可以取任何值的变量。

///////////////////////////////////////////

解决方案

棘手的解决方案是操纵状态并将其与返回的 URL 一起注入,因为它会在 login/signup 响应后发回。创建登录Url() 方法:

let url = that.loginUrl
                        + '?response_type='
                        + response_type
                        + '&client_id='
                        + encodeURIComponent(that.clientId)
                        + '&state='
                        + encodeURIComponent((state) + 'url'  + returnedUrl)
                        + '&redirect_uri='
                        + encodeURIComponent(window.location.origin)
                        + '&scope='
                        + encodeURIComponent(that.scope);

所以这里我把state用'url'字拆分了,等有响应了再看一遍。

encodeURIComponent((state) + 'url' + returnedUrl)

一个重要的细节redirect_uri,应该同源:

'&redirect_uri=' + encodeURIComponent(window.location.origin)

并且这个 URL 应该添加到 Azure B2C 应用程序中返回的 URL。

现在我可以在 tryLogin() 方法中再次拆分它:

const statePartsWithUrl = (parts['state'] + '').split('url');
window.location.href = statePartsWithUrl[1];

而且效果很好。

////-------------------------------------

编辑:1.2.2019

const statePartsWithUrl = (parts['state'] + '').split('url');
let state = '';
let returnedUrl = '';
if (statePartsWithUrl != null) {
  state = statePartsWithUrl[0];
  returnedUrl = statePartsWithUrl[1];
}

这是在方法 tryLogin(options)

中拆分状态以从中读取信息

是的,正如您发现的那样,您目前无法在 B2C 中添加通配符以回复 URL。

这可能是由于 OAuth 2.0 Threat Model and Security Considerations RFC 中定义的安全问题。 其中,针对开放重定向攻击的建议对策是让客户端注册完整的重定向 URI。

也无法以编程方式创建应用程序:https://feedback.azure.com/forums/169401-azure-active-directory/suggestions/19975480-programmatically-register-b2c-applications

很遗憾,手动方式是目前唯一的方式。但请务必在 User Voice 上对功能请求进行投票。

我什至尝试通过 Graph Explorer 手动编辑应用程序:

{
    "odata.error": {
        "code": "Request_BadRequest",
        "message": {
            "lang": "en",
            "value": "Updates to converged applications are not allowed in this version."
        },
        "date": "2018-01-08T12:00:00",
        "requestId": "208e7159-d459-42ec-8bb7-000000000000",
        "values": null
    }
}

正如您在评论中所建议的那样,解决此问题的一种方法是使用单个静态重定向 URI 并将 language/culture 保留在 state/a cookie 中,然后执行重定向在用户返回到应用程序后转到特定语言版本。