Microsoft.Owin.Security Google Challenge仅适用于ReturnUrl

时间:2017-05-05 15:29:30

标签: .net vb.net katana

我的Google身份验证实施中遇到了一个奇怪的问题。我有一个vb.net Web表单应用程序,它利用Microsoft.Owin.Security来处理Google身份验证。该过程正常,但只有在浏览器登录页面中有ReturnUrl时才可以。如果系统无法通过电子邮件找到匹配项,我现在正在重定向。如果我在没有ReturnUrl的情况下重定向,则下一次尝试不起作用。我不确定为什么这只是使用此查询参数。

工作网址:http://local.template.com/Security/Login.aspx?ReturnUrl=%2fSecurity%2fLogin.aspx

无工作网址:http://local.template.com/Security/Login.aspx#

启动类:

Imports Owin
Imports Microsoft.AspNet.Identity
Imports Microsoft.Owin
Imports Microsoft.Owin.Security.Google
<Assembly: OwinStartup(GetType(Startup))>
Partial Public Class Startup
    ' For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301883
    Public Sub Configuration(app As IAppBuilder)
        ' Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie)

        Dim googleOptions = New GoogleOAuth2AuthenticationOptions() With {
            .ClientId = ConfigurationManager.AppSettings("Google.ClientID"),
            .ClientSecret = ConfigurationManager.AppSettings("Google.Secret")
            }
          googleOptions.Provider = New GoogleOAuth2AuthenticationProvider() 

        googleOptions.Scope.Add("openid")
        googleOptions.Scope.Add("profile")
        googleOptions.Scope.Add("email")

        app.UseGoogleAuthentication(googleOptions)


    End Sub

End Class

登录点击:

Public Sub btnGoogle_Click(sender As Object, e As EventArgs) 

        Dim provider = "Google"
        ' Request a redirect to the external login provider
        Dim redirectUrl As String = ResolveUrl([String].Format(CultureInfo.InvariantCulture, "{0}&provider={1}", RedirectPageUrl,provider))

        Dim properties As AuthenticationProperties = New AuthenticationProperties() With {.RedirectUri = redirectUrl}

        Context.GetOwinContext().Authentication.Challenge(properties, provider)
        Response.StatusCode = 401
        Response.[End]()

End Sub

无法匹配重定向:

    ''' <summary>
    '''     Signs out the external cookie and redirects
    ''' </summary>
    Private Sub SignOutExternalAuthAndRedirect(Optional ByVal userNotFound As Boolean = False,
                                               Optional ByVal signedInEmailAddress As String = "")
        Dim authManager = Context.GetOwinContext().Authentication
        authManager.SignOut(DefaultAuthenticationTypes.ExternalCookie)
        Dim errormessage = "Something went wrong while trying to sign in with Google"
        If userNotFound Then _
            errormessage = "Invalid login. There is no user account associated with email address " &
                           signedInEmailAddress
        JGrowl.ShowMessage(JGrowlMessageType.Error, errormessage, isDelayed := True)
        RedirectOnFail()
    End Sub

    ''' <summary>
    '''     redirect to login page or default page if signedin
    ''' </summary>
    Private Sub RedirectOnFail()

        Response.Redirect("~/Security/Login.aspx")

    End Sub

0 个答案:

没有答案