以下代码会导致未定义的行为:
class T
{
public:
const std::string& get() const { return s_; }
private:
std::string s_ { "test" };
}
void breaking()
{
const auto& str = T{}.get();
// do sth with "str" <-- UB
}
Run Code Online (Sandbox Code Playgroud)
(因为const&根据我的理解,生命周期延长不适用于此处)。
为了防止这种情况,一种解决方案可能是添加一个引用限定符get()以防止在 LValues 上调用它:
const std::string& get() const & { return s_; }
Run Code Online (Sandbox Code Playgroud)
但是,由于函数现在既是const又是&合格的,因此仍然可以调用get()RValues,因为它们可以分配给const&:
const auto& t = T{}; // OK
const auto& s1 = t.get(); // OK
const auto& s2 = …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个项目,尝试设置一个基于 IdentityServer4 ( https://github.com/IdentityServer/IdentityServer4 ) 的服务,该服务通过 LDAP 查询本地 Active Directory 来对用户进行身份验证。
为了实现这一目标,我还在我的项目中包含了 IdentityServer4.LdapExtension ( https://github.com/Nordes/IdentityServer4.LdapExtension )。存储库中的工作示例工作正常(https://github.com/Nordes/IdentityServer4.LdapExtension/tree/master/Sample/IdentityServer) - 但自定义逻辑是 UI 的一部分,我需要我的服务在没有任何用户界面。
只需添加
.AddLdapUsers<ActiveDirectoryAppUser>(Conf.GetSection("ldap"), UserStore.InMemory)
Run Code Online (Sandbox Code Playgroud)
如文档中所述,不会更改请求管道,因为所提供的登录/验证方法永远不会执行 - 它们仅通过来自 UI (AccountController) 的调用来触发。但是,正如我所说,我不想在此服务中集成任何 UI,而是使用 Token-Endpoint 已提供的接口(使用 client_id 和 client_secret 进行 POST 请求,使用 JWT 进行响应)。
有没有一种方法可以集成 LDAP 身份验证,而无需重写按需要开箱即用的大部分内容?