Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net AuthenticationProperties.RedirectUri未在质询()中传递给Google_Asp.net_Asp.net Mvc_Owin_Katana_Google Signin - Fatal编程技术网

Asp.net AuthenticationProperties.RedirectUri未在质询()中传递给Google

Asp.net AuthenticationProperties.RedirectUri未在质询()中传递给Google,asp.net,asp.net-mvc,owin,katana,google-signin,Asp.net,Asp.net Mvc,Owin,Katana,Google Signin,在我的web应用程序中,我已将Google注册为单一登录提供商: app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions { ClientId = "8765.......apps.googleusercontent.com", ClientSecret = "Secret" }) 我的应用程序不允许用户注册(他们的帐户是由管理员创建的,但他们可以稍后将其帐户与谷歌链接) 在我的“使用谷歌登录”控制器中

在我的web应用程序中,我已将Google注册为单一登录提供商:

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions {
    ClientId = "8765.......apps.googleusercontent.com",
    ClientSecret = "Secret"
})
我的应用程序不允许用户注册(他们的帐户是由管理员创建的,但他们可以稍后将其帐户与谷歌链接)

在我的“使用谷歌登录”控制器中,我试图发出一个
Challenge()
重定向到谷歌。这可能不是正确的方法:

string redirectUri = "http://localhost:55262/SSO/Google/ProcessToken"; // actually created in code, but shown as string for clarity
AuthenticationProperties properties = new AuthenticationProperties();
properties.RedirectUri = Server.UrlEncode(redirectUri);
Context.GetOwinContext().Authentication.Challenge(properties, "Google");
这会正确地将用户发送到Google,但Google随后会显示错误:重定向\u uri\u不匹配,说明:

请求中的重定向URI: 与注册的重定向URI不匹配

当Google控制面板中的返回URI集合不包含指定的
redirect\u URI
时,我以前见过这个错误

如果我在VS2015中调试,我可以看到
AuthenticationProperties
中正确设置了
重定向uri
属性,但似乎OWIN/Katana没有将其传递给Google。相反,当我点击Google时,返回的uri是OWIN/Katana使用的默认uri。我设置的那个被忽略了

谷歌的请求详情似乎证实了这一点:

scope=openid profile email
response_type=code
redirect_uri=http://localhost:55262/signin-google

请问我做错了什么?我是否应该使用
Challenge()
来允许用户将其本地应用程序帐户与谷歌链接

注意OWIN的开放式身份验证具有预定义的方法。换句话说,在
localhost:port/signin-google
中,OWIN等待外部身份验证服务调用signin-google(尽管您在项目中找不到它的实现)。登录google是一个有效的工作路径,我预告您不要更改它(因为避免将新的实现编写为控制器操作)

我也遇到过类似的问题,在度过了许多疲惫的日子之后,我终于发现问题来自原始用户的URL,该URL在OWIN发送的
重定向uri
上有效。显然:

  • 如果您键入www.site.com→ <代码>重定向\u uri等于
    www.site.com/signin google
  • 如果您键入site.com→ <代码>重定向\u uri等于
    site.com/signin google

谷歌将根据控制台中输入的重定向URL返回上述情况之一的
redirect\u uri\u mismatch
Error。我认为您的问题也来自这个现实,解决方案是在控制台中设置任何可能的URL。

提供有关已接受答案的其他信息

谷歌可以忽略
/sign
结果显示,
/signin google
URI由OWIN/Katana内部管理。作为一名开发人员,您不需要关心它,但是您需要将它作为授权重定向URI添加到Google开发人员控制台中

在Google请求中,请注意,无论您在
AuthenticationProperties.RedirectUri
属性中设置了什么自定义URI,OWIN总是将重定向URI作为
/signin Google
传递给Google。虽然一开始这看起来像是一个bug/问题,但它有一个主要优势,即OWIN可以通过单个回调URI管理所有回调。您的回调URI也不会被忘记(见下文)

那么你自己的重定向URL呢? 这就是
AuthenticationProperties()
发挥作用的地方。通过像这样指定自己的回调URL

AuthenticationProperties properties = new AuthenticationProperties { RedirectUri = "https://my.app.com/custom/callback/uri" };
…OWIN检查Google令牌并提取必要的详细信息后,用户将被重定向到指定的URL

这就是我感到困惑的地方,因为我不知道如何处理
/signin google
,而实际上没有采取任何行动。这适用于MVC和webforms,您不需要担心传递给Google的内容。但是,如果使用webforms或在web.config中指定授权规则,则需要这样做以防止返回的用户再次访问日志页面:

<location path="signin-google">
    <system.web>
        <authorization>
            <allow users="*"/>
        </authorization>
    </system.web>
</location>
入境 用户在验证身份后从Google返回

Microsoft.AspNet.Identity.Owin.ExternalLoginInfo loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();

什么是GoogleOAuth2AuthenticationOptions?遗憾的是,OWIN Katana没有使用Google的完整示例。它是一个OWIN类对象,包含Google OAuth2身份验证所需的字段。它只用于谷歌,naturally.Response.StatusCode=401;Response.End();这是关键的修改。谢谢,这花了一些时间才找到。这是一个完整的雷区,不是吗-/
Microsoft.AspNet.Identity.Owin.ExternalLoginInfo loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();