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

时间:2015-02-24 19:36:38

标签: 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>

它发布到这个方法: 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" }));
}

回调落在这里:

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 });
...

第二行称之为: 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;
...

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

重现:

我一直想知道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);
        }
    }
}

有什么想法吗?

0 个答案:

没有答案