What is the idea behind IIdentity and IPrincipal in .NET

前端 未结 4 1507
清歌不尽
清歌不尽 2021-01-30 16:23

So, what is the purpose for existence of both IIdentity and IPrincipal, and not some IIdentityMergedWithPrincipal? When is it not enough t

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 17:00

    IIdentity is just used for the user's authenticated identity, regardless of what roles they may have.

    IPrincipal is used to combine a user's identity with the authorized roles they have in a given security context.

    For example, you can use a third-party login provider, like Facebook or Google, to get the user's identity, but you will not get a principal from those providers, as they don't provide any roles. You can use your own application or a third-party role-based authorization provider to apply roles to, say, a FacebookIdentity or GoogleIdentity. A different application can expect a different principal, with its own roles, but still use the same identity as in another application.

提交回复
热议问题