使用 asp.net MVC 使用 Google 重定向 URI
Redirect URI with Google using asp.net MVC
我正在尝试在 ASP.NET MVC 5 中将 oauth 与 Google 结合使用。
在 Google 的开发者控制台中,我输入了重定向 uri:
www.mydomain.com/account/externallogincallback
并认为这样就可以了。但是没有。
我输入:
www.mydomain.com/signin-google
成功了!
我试图在我的项目中搜索字符串 "signin-google",但在任何地方都找不到。
谁能告诉我这是怎么回事?为什么?谢谢。
我懒得写一个格式正确的答案,我把这些评论放在代码中让自己记住如何解决这个问题。这不是一个真正的问题,只是我从来没有费心去正确阅读的东西 :) 但这是你可以做的让它工作的方法。有 2 个选项供您选择。我都试过了,这两个选项都很好用。我暂时选择了第一个,这真的没关系。这是我在 Startup.Auth.cs 文件中的评论。
// My notes to resolve Google Error: redirect_uri_mismatch error
// By default GoogleOAuth2AuthenticationOptions has CallbackPath defined as "/signin-google"
// https://msdn.microsoft.com/en-us/library/microsoft.owin.security.google.googleoauth2authenticationoptions(v=vs.113).aspx
// But the real path should be Controller/Action: for this application it is "/Account/ExternalLoginCallback"
// There are 2 ways to define it properly:
// 1) Add a new route in RouteConfig.cs that will map "/signin-google" into "/Account/ExternalLoginCallback":
// routes.MapRoute(name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "ExternalLoginCallback" });
// Remember, in Google Developers Console you must have your "/signin-google" redirect URI, since that is what your app sends to Google
// 2) Completely overwrite built-in "/signin-google" path.
// Owerwrite CallbackPath right here by adding this line after ClientSecret:
// CallbackPath = new PathString("/Account/ExternalLoginCallback")
// Remember, in Google Developers Console you must have "/Account/ExternalLoginCallback" redirect URI, since now that is what your app sends to Google
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "xxxxxxxxxxxxxxxxxxxx",
ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxx"
});
我正在尝试在 ASP.NET MVC 5 中将 oauth 与 Google 结合使用。
在 Google 的开发者控制台中,我输入了重定向 uri:
www.mydomain.com/account/externallogincallback
并认为这样就可以了。但是没有。
我输入:
www.mydomain.com/signin-google
成功了!
我试图在我的项目中搜索字符串 "signin-google",但在任何地方都找不到。
谁能告诉我这是怎么回事?为什么?谢谢。
我懒得写一个格式正确的答案,我把这些评论放在代码中让自己记住如何解决这个问题。这不是一个真正的问题,只是我从来没有费心去正确阅读的东西 :) 但这是你可以做的让它工作的方法。有 2 个选项供您选择。我都试过了,这两个选项都很好用。我暂时选择了第一个,这真的没关系。这是我在 Startup.Auth.cs 文件中的评论。
// My notes to resolve Google Error: redirect_uri_mismatch error
// By default GoogleOAuth2AuthenticationOptions has CallbackPath defined as "/signin-google"
// https://msdn.microsoft.com/en-us/library/microsoft.owin.security.google.googleoauth2authenticationoptions(v=vs.113).aspx
// But the real path should be Controller/Action: for this application it is "/Account/ExternalLoginCallback"
// There are 2 ways to define it properly:
// 1) Add a new route in RouteConfig.cs that will map "/signin-google" into "/Account/ExternalLoginCallback":
// routes.MapRoute(name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "ExternalLoginCallback" });
// Remember, in Google Developers Console you must have your "/signin-google" redirect URI, since that is what your app sends to Google
// 2) Completely overwrite built-in "/signin-google" path.
// Owerwrite CallbackPath right here by adding this line after ClientSecret:
// CallbackPath = new PathString("/Account/ExternalLoginCallback")
// Remember, in Google Developers Console you must have "/Account/ExternalLoginCallback" redirect URI, since now that is what your app sends to Google
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "xxxxxxxxxxxxxxxxxxxx",
ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxx"
});