我在开源项目中调试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可以是Google,Facebook等程序包中IAuthenticationManager的任何实现。问题在于它们都失败并返回空对象,因此应用程序无法登录用户。

复制:
  • 克隆v13分支:https://github.com/POPWorldMedia/POPForums.git
  • 按照说明在本地运行应用程序:https://github.com/POPWorldMedia/POPForums/wiki/Documentation
  • 在管理员中,选择外部登录页面,然后为提供者之一添加凭据,然后选中该框。
  • 注销,然后使用按钮登录新的提供者。
  • 观看它失败,并查看上述ExternalAuthentication GetAuthenticationResult方法的authResult为空。

  • 我一直想知道我不了解的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);
            }
        }
    }
    

    有任何想法吗?

    最佳答案

    不确定这是否有帮助,但是如果您查看模板,请使用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);
    }
    

    而且看起来您的启动代码与默认模板略有不同。

    您正在使用:
    app.SetDefaultSignInAsAuthenticationType(ExternalAuthentication.ExternalCookieName);
    

    默认模板使用的位置:
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    

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

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

    听到这让您感到沮丧的消息令人非常沮丧-这些东西应该向后兼容-使用这样的核心系统组件破坏现有的代码是不正确的。

    关于c# - 将OWIN从2.1更新到3.0.1会中断外部身份验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28704700/

    10-13 06:24