将OWIN从2.1更新到3.0.1会破坏外部身份验证

Jef*_*utz 8 c# asp.net-mvc owin

我在开源项目中调试OWIN包升级时遇到了困难.简短的描述是当我从v2.1升级时,在新的v3版本中使用外部登录会中断,并且在调试过程中我无法弄清楚有什么不同.请记住,我的代码都没有更改,我只更新了OWIN组件(Microsoft.Owin中的软件包和其他子名称空间).

它从这个表格帖子开始:

<form action="/Forums/Authorization/ExternalLogin?ReturnUrl=http%3A%2F%2Flocalhost%3A1973%2FForums" method="post"><input name="__RequestVerificationToken" type="hidden" value="--verificationtoken--" />       <h2>External Logins</h2>
   <p>
    <button type="submit" id="Google" name="provider" value="Google" class="btn btn-primary">Google</button>
   </p>
</form>
Run Code Online (Sandbox Code Playgroud)

它发布到这个方法:https: //github.com/POPWorldMedia/POPForums/blob/v13.0.0/PopForums/Controllers/AuthorizationController.cs

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
    return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Authorization", new { loginProvider = provider, ReturnUrl = returnUrl, area = "PopForums" }));
}
Run Code Online (Sandbox Code Playgroud)

回调落在这里:

public async Task<ActionResult> ExternalLoginCallback(string loginProvider, string returnUrl)
{
    var authentication = _owinContext.Authentication;
    var authResult = await _externalAuthentication.GetAuthenticationResult(authentication);
    if (authResult == null)
        return RedirectToAction("Login", "Account", new { error = Resources.ExpiredLogin });
...
Run Code Online (Sandbox Code Playgroud)

第二行称为:https: //github.com/POPWorldMedia/POPForums/blob/v13.0.0/PopForums/ExternalLogin/ExternalAuthentication.cs

public async Task<ExternalAuthenticationResult> GetAuthenticationResult(IAuthenticationManager authenticationManager)
{
    var authResult = await authenticationManager.AuthenticateAsync(ExternalCookieName);
    if (authResult == null)
        return null;
...
Run Code Online (Sandbox Code Playgroud)

AuthenticationManager可以是IAuthenticationManager的任何实现,包括Google,Facebook等.问题是它们都失败并返回一个null对象,因此app无法登录用户.

重现:

我一直想知道OWIN配置中是否有一些我不明白的变化.为了记录,就在这里:https://github.com/POPWorldMedia/POPForums/blob/v13.0.0/PopForums/Configuration/PopForumsOwinStartup.cs

using System;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Owin;
using PopForums.ExternalLogin;
using PopForums.Services;
using PopForums.Web;

namespace PopForums.Configuration
{
    public class PopForumsOwinStartup
    {
        public void Configuration(IAppBuilder app)
        {
            var setupService = PopForumsActivation.ServiceLocator.GetInstance<ISetupService>();
            if (!setupService.IsDatabaseSetup())
                return;

            var settings = PopForumsActivation.ServiceLocator.GetInstance<ISettingsManager>().Current;

            app.SetDefaultSignInAsAuthenticationType(ExternalAuthentication.ExternalCookieName);

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = ExternalAuthentication.ExternalCookieName,
                AuthenticationMode = AuthenticationMode.Passive,
                CookieName = CookieAuthenticationDefaults.CookiePrefix + ExternalAuthentication.ExternalCookieName,
                ExpireTimeSpan = TimeSpan.FromMinutes(60)
            });

            if (settings.UseTwitterLogin)
                app.UseTwitterAuthentication(
                   consumerKey: settings.TwitterConsumerKey,
                   consumerSecret: settings.TwitterConsumerSecret);

            if (settings.UseMicrosoftLogin)
                app.UseMicrosoftAccountAuthentication(
                    clientId: settings.MicrosoftClientID,
                    clientSecret: settings.MicrosoftClientSecret);

            if (settings.UseFacebookLogin)
                app.UseFacebookAuthentication(
                   appId: settings.FacebookAppID,
                   appSecret: settings.FacebookAppSecret);

            if (settings.UseGoogleLogin)
                app.UseGoogleAuthentication(settings.GoogleClientId, settings.GoogleClientSecret);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Ric*_*ahl 2

不确定这是否有帮助,但如果您查看模板,请使用 AuthenticationManager.ExternalLinkLoginInfoAsync() 检索 OAuth 回调的结果。你能检查一下我吗

[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ExternalLinkLogin(string provider) //Google,Twitter etc.
{
    return new ChallengeResult(provider, Url.Action("ExternalLinkLoginCallback"), userId);
}

[AllowAnonymous]
[HttpGet]        
public async Task<ActionResult> ExternalLinkLoginCallback()
{
    // Handle external Login Callback
    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(XsrfKey,userId);
    if (loginInfo == null)
    {
        IdentitySignout(); // to be safe we log out
        return RedirectToAction("Register", new {message = "Unable to authenticate with external login."});
    }

    ...

    IdentitySignIn(userId, userName, returnUrl);
}
Run Code Online (Sandbox Code Playgroud)

另外,您的启动代码似乎与默认模板略有不同。

您正在使用:

app.SetDefaultSignInAsAuthenticationType(ExternalAuthentication.ExternalCookieName);
Run Code Online (Sandbox Code Playgroud)

默认模板使用的位置:

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

您可以在我的一篇博客文章的摘要中快速比较模板使用的内容:

http://weblog.west-wind.com/posts/2015/Apr/29/Adding-minimal-OWIN-Identity-Authentication-to-an-Existing-ASPNET-MVC-Application#MinimalCodeSummary

听到这对你来说很令人沮丧——这个东西应该向后兼容——使用这样的核心系统组件来破坏现有代码是不行的。