在我的web应用程序中,我已经将Google注册为一个单一的登录提供商:
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions {
ClientId = "8765.......apps.googleusercontent.com",
ClientSecret = "Secret"
})我的应用程序不允许用户注册/注册(相反,他们的帐户是由管理员创建的,但是他们以后可以将帐户链接到Google)。
在我的“与谷歌签约”控制器,我试图发布一个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随后显示了错误: redirect_uri_mismatch,它说:
请求中的重定向URI:http://localhost:55262/signin-google与注册的重定向URI不匹配。
当谷歌控制面板中的返回URI集合不包含指定的redirect_uri时,我以前见过这个错误。
如果我在VS2015中调试,我可以看到redirect_uri属性在AuthenticationProperties中被正确设置,但是看起来OWIN/Katana并没有将它传递给谷歌。相反,当我点击谷歌时,return_uri是OWIN/Katana使用的默认版本。我设定的那个被忽略了。
谷歌的请求细节似乎证实了这一点:
scope=openid profile email
response_type=code
redirect_uri=http://localhost:55262/signin-google我在这里做错什么了?我不应该使用Challenge()让用户连接他们的本地应用程序帐户与谷歌?
发布于 2015-11-16 10:20:22
注意到,OWIN的开放身份验证有预定义的方法。换句话说,在localhost:port/signin-google中,OWIN在等待外部身份验证服务调用signin(尽管您在项目中找不到它的实现)。signin-google是一条有效且有效的路径,我预先告诫您不要更改它(因为要避免将新的实现作为控制器操作编写)。
我也遇到了类似的麻烦,在花了很多天的时间后,我终于发现这个问题来自于原来的用户的URL,它对自己发送的redirect_uri是有效的。显然:
redirect_uri等于www.site.com/signin-googleredirect_uri等于site.com/signin-google谷歌将根据控制台中输入的重定向URL返回上述情况之一的redirect_uri_mismatch Error。我认为您的问题也来自于这个现实,的解决方案是在控制台中设置任何可能的URL。
发布于 2015-11-16 15:36:04
提供关于已接受的答案的补充信息..。
无视/signin-google是没问题的
可以看出,/signin-google URI是由OWIN/Katana内部管理的。作为开发人员,您不需要担心它,但是您需要将它作为授权的重定向URI添加到Google控制台中。
在Google请求中,请注意,OWIN总是将重定向URI作为/signin-google传递给Google,而不管您在AuthenticationProperties.RedirectUri属性中设置了什么自定义URI。虽然起初这似乎是一个bug/问题,但它有一个主要的优势,即OWIN可以通过一个回调URI来管理所有回调。您的回调URI也不会被忘记(见下面)!
那么您自己的重定向URL呢?
这就是AuthenticationProperties()发挥作用的地方。通过指定您自己的回调URL如下..。
AuthenticationProperties properties = new AuthenticationProperties { RedirectUri = "https://my.app.com/custom/callback/uri" };Google检查了...after令牌并提取了必要的详细信息,然后将用户重定向到指定的URL。
这就是我感到困惑的地方,因为我不知道如何处理/signin-google,而实际上并没有采取任何行动。这既适用于MVC,也适用于webforms --您不需要担心传递给Google的内容。但是,如果使用webforms或在web.config中指定授权规则,则需要这样做以防止返回用户再次访问日志记录页:
<location path="signin-google">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>下面是将用户发送到Google并返回包含其详细信息的标记所需的所有代码:
出站
将用户从控制器发送到Google,单击事件按钮,页面加载,任何内容(不管您的ASP/主机堆栈如何):
// set the callback, for after OWIN finishes examining what comes back from Google
AuthenticationProperties properties = new AuthenticationProperties { RedirectUri = "https://www.myapp.com/some/callback/uri" };
// send the user to Google
Context.GetOwinContext().Authentication.Challenge(properties, "Google");
// Stop execution of the current page/method - the 401 forces OWIN to kick-in and do its thing
Response.StatusCode = 401;
Response.End();入站
用户在验证身份后从Google返回。
Microsoft.AspNet.Identity.Owin.ExternalLoginInfo loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();https://stackoverflow.com/questions/33698266
复制相似问题