gpt4 book ai didi

c# - User 类是否应该实现 IPrincipal 和 IIdentity

转载 作者:太空狗 更新时间:2023-10-30 01:34:50 25 4
gpt4 key购买 nike

我正在为 asp.net web api 2 构建我自己的基于 token 的身份验证,我对 user 类有疑问。

目前我的 user 类实现了 IPrincipalIIdentity,但我不知道什么是最佳实践。您是否会将 IPrincipalIIdentity 分开并使 IIdentity 实现成为 IPrincipal 的成员?

目前我使用下面的类声明

class User : IPrincipal, IIdentity

谢谢!

最佳答案

Would you divide IPrincipal and IIdentity and make the IIdentity implementation a member of IPrincipal?

我认为这不是一个好方法,因为如果 IdentityIPrincipal 相似,则不应该有任何分离的理由。

此外,截至目前,您的类实现了这两个接口(interface)。这意味着您的类实现了两个接口(interface)的方法。如果您稍后出于任何原因决定您的类不应该实现一个或另一个接口(interface),您应该只需要删除绑定(bind)到您要删除的接口(interface)的方法的实现。

除上述内容外,请记住以下几点:

The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use

这是五个 SOLID 之一原则,这是面向对象编程和设计的一些基本原则。

关于c# - User 类是否应该实现 IPrincipal 和 IIdentity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28678671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com