ASP.NET OWIN中外部登录的外部Cookie

cuo*_*gle 7 asp.net-mvc owin asp.net-mvc-5 azure-active-directory openid-connect

我们有一个基于ASP.NET Mvc 4的遗留系统,现在我们希望通过Azure Active Directory为当前用户和新用户支持Signal Sign On.由于我们已经管理了自己的身份验证工作流,因此ASP.NET身份确实不适合我们的情况.

我已经设法构建了一个演示,该演示正在使用OWIN OpenIdConnect中间件被动模式,而不使用ASP.NET身份.以下代码正常工作:

app.SetDefaultSignInAsAuthenticationType("ExternalCookie");
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = "ExternalCookie",
    AuthenticationMode = AuthenticationMode.Passive,
});

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
    {
        AuthenticationMode = AuthenticationMode.Passive,
        ClientId = ClientId,
        Authority = Authority

        // More code
    });
Run Code Online (Sandbox Code Playgroud)

并在ExternalLoginCallback行动:

public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var authManager = Request.GetOwinContext().Authentication;

    var result = await authManager.AuthenticateAsync("ExternalCookie");
    authManager.SignOut("ExternalCookie");

    //More code to convert to local identity
}
Run Code Online (Sandbox Code Playgroud)

即使使用Google,Facebook或Twitter等其他提供商,这种情况也很常见.我不太清楚的一件事是ExternalCookie,也许我错过了整件事.我的理解是,当外部登录成功时,外部cookie用于存储外部声明身份.然后我们打电话给:

var result = await authManager.AuthenticateAsync("ExternalCookie");
authManager.SignOut("ExternalCookie");
Run Code Online (Sandbox Code Playgroud)

为了获得外部声明身份,然后将外部身份转换为本地身份.我有点困惑为什么SignOut在这种情况下我们必须调用外部cookie.

此外,我不确定在使用外部登录时是否必须使用外部Cookie,或者我们是否有其他方法而不使用外部Cookie.

请有人就这一点作出解释.

小智 1

为了回答你的最后一个问题,你可以在配置外部cookie的startup.auth文件中更改cookie的名称 -

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
Run Code Online (Sandbox Code Playgroud)

您可以使用字符串代替 DefaultAuthenticationTypes 枚举并直接指定 cookie 的名称,例如 -

app.UseExternalSignInCookie("myExternalCookie");
Run Code Online (Sandbox Code Playgroud)