SignInManager.IsSignedIn(User)与User.Identity.IsAuthenticated

Mon*_*mer 0 c# asp.net asp.net-core asp.net-core-identity

_LoginPartial.cshtmlAsp.net Core Web应用程序模板提供的默认值如下。

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

@if (SignInManager.IsSignedIn(User))
{
    <form asp-controller="Account" asp-action="Logout" method="post">
        <ul >
            <li>
                <a asp-controller="Manage" asp-action="Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
            </li>
            <li>
                <button type="submit">Log out</button>
            </li>
        </ul>
    </form>
}
else
{
    <ul >
        <li><a asp-controller="Account" asp-action="Register">Register</a></li>
        <li><a asp-controller="Account" asp-action="Login">Log in</a></li>
    </ul>
}
Run Code Online (Sandbox Code Playgroud)

而不是使用注入SignInManager.IsSignedIn(User),我们为什么不使用User.Identity.IsAuthenticated它简单得多?有没有我没有注意到的区别?

blo*_*art 5

IsAuthenticated可以处理所有类型的ClaimsPrincipals,它们可能来自ASP.NET Core身份验证,社交身份验证,AAD,WS-Fed或其他任何方式。

IsSignedIn非常特定于ASP.NET Identity。

如果仅使用ASP.NET Identity,请坚持使用IsSignedIn。如果您编写的应用程序可以使用其他类型的身份验证,请使用IsAuthenticated。