簡體   English   中英

外部身份驗證不會重定向到外部站點

[英]External Authentication not redirecting to external site

在這里發生了一件奇怪的事情。

我已經構建了一個ASP.NET MVC5網站,並通過ASP.NET身份使本地帳戶正常工作。

我現在正在嘗試啟用外部身份驗證,但發生了一些奇怪的事情。

我確定我已經按照正確的步驟。 我在我的Startup.Auth.cs中有這個:

    public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Uncomment the following lines to enable logging in with third party login providers
        //app.UseMicrosoftAccountAuthentication(
        //    clientId: "",
        //    clientSecret: "");

        //app.UseTwitterAuthentication(
        //   consumerKey: "",
        //   consumerSecret: "");

        //app.UseFacebookAuthentication(
        //   appId: "",
        //   appSecret: "");

        app.UseGoogleAuthentication();
    }

當用戶單擊要使用Google登錄的鏈接時,將調用ExternalLogin方法:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult ExternalLogin(string provider, string returnUrl)
    {
        // Request a redirect to the external login provider
        return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
    }

我通過調試驗證的是進入ChallengeResult類的ExecuteResult方法:

        public override void ExecuteResult(ControllerContext context)
        {
            var properties = new AuthenticationProperties() { RedirectUri = RedirectUri };
            if (UserId != null)
            {
                properties.Dictionary[XsrfKey] = UserId;
            }
            context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
        }

但是,在瀏覽器中,沒有任何反應。 我只是得到一個空白頁面,我希望重定向到Google登錄頁面。

根本沒有報告錯誤。

另一個有趣的事情是我嘗試創建另一個MVC5應用程序,但我在VS2013中得到一個“對象引用沒有設置為對象的實例”彈出窗口,並且生成的項目缺少默認情況下通常存在的帳戶控制器。

我已經修復了VS2013的安裝,我重新安裝了Update 1,並且我還更新了解決方案中的所有Nuget軟件包。

我已經沒有了下一步的想法。

更新1認為這可能與我的PC有關,我已將網站部署到Azure,問題仍然存在。 這是否意味着它可能與缺少的裝配有關,並且沒有正確報告? 我已經運行了fusionlog,但我沒有看到綁定失敗。

通過干凈安裝Windows 8和VS2013來啟動新VM,看看我是否可以在那里運行新項目。

更新2好了,剛剛運行了另一輪“網絡”捕獲,當用戶選擇外部提供商時,它會發布到ExternalLogin操作,但響應是401 Unauthorized。 可能是什么導致了這個?

好,

我弄清楚問題是什么(重要的部分)。

首先,我仍然不知道為什么當我創建一個MVC項目時,我沒有得到一個腳手架的AccountController類。

但是,似乎我的問題是我的登錄按鈕是通過“谷歌”而不是“谷歌”作為提供者。

說真的!? 套管對提供商的名稱很重要,我有點驚訝,但你去了。

這不是你的問題的答案,但它回應了一個非常類似的問題

如果你有Owin 2.0並且你遷移到2.1

_ExternalLoginsListPartial需要更改:

<button type="submit" class="btn btn-default" id="@p.AuthenticationType" name="Partner" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button>

對此

<button type="submit" class="btn btn-default" id="@p.AuthenticationType" name="provider" value="@p.AuthenticationType" title="Log in using your @p.Caption account">@p.AuthenticationType</button>

唯一改變的是按鈕的名稱,但這可能像我一樣打破你的頭,花了我2​​天的工作來找到問題。

也許這是在新的Owin版本中解釋的,如果不是必須要在大寫字母中指定。

我剛剛遇到了與ASP.Net WebAPI服務器相同的問題。 我將正確的AuthenticationType傳遞給挑戰方法,但仍然得到一個空白頁面。

原來問題是我在啟動時將OAuth提供程序添加到OWIN管道中的順序。 大致我的工作順序:

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        OAuthBearerOptions = new OAuthBearerAuthenticationOptions();

        var authServerOptions = new OAuthAuthorizationServerOptions
        {
        ...
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(authServerOptions);
        app.UseOAuthBearerAuthentication(OAuthBearerOptions);

        // Configure Google External Login
        GoogleAuthOptions = new GoogleOAuth2AuthenticationOptions()
        {
            ClientId = xxxx,
            ClientSecret = xxxx,
            Provider = new GoogleAuthProvider()
        };
        app.UseGoogleAuthentication(GoogleAuthOptions);

        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);

以前我在UseGoogleAuthentication方法之前添加了UseCors + UseWebApi方法。

我發現這篇文章也很有用: http//coding.abel.nu/2014/06/understanding-the-owin-external-authentication-pipeline/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM