【问题标题】:Redirect URI with Google using asp.net MVC使用 asp.net MVC 使用 Google 重定向 URI
【发布时间】:2016-01-12 10:31:36
【问题描述】:

我正在尝试在 ASP.NET MVC 5 中将 oauth 与 Google 一起使用。

在 Google 的开发者控制台中,我输入了重定向 uri:

www.mydomain.com/account/externallogincallback

并认为这会做。但它没有。

我说:

www.mydomain.com/signin-google

它成功了!

我尝试在我的项目中搜索字符串“signin-google”,但在任何地方都找不到。

谁能告诉我这是怎么回事?为什么呢?谢谢。

【问题讨论】:

  • 如果你把 google 控制台那个 url 你想如何出现在你的 MVC 项目中? “它有效”是什么意思,谷歌控制台让你保存网址,或者你可以用谷歌帐户登录你的网站?我怀疑女巫会起作用。
  • @SilentTremor 感谢您的评论。我不得不将 signin-google 放在我的 Google Developer Console 中,而不是 MVC 的默认帐户控制器中的 ExternalLoginCallback。它工作意味着我能够登录。令我惊讶的是,我也理解你的。我希望有人向我解释这个问题,因为我打算在我的网站上添加更多的 OAuth 提供程序,并且我想完全理解这个问题。

标签: c# asp.net-mvc oauth-2.0 google-authentication


【解决方案1】:

我懒得写一个格式正确的答案,我把这些 cmets 放在代码中让自己记住如何解决这个问题。这不是一个真正的问题,只是我从来没有费心去正确阅读的东西:) 但这是你可以做的让它工作。有 2 个选项可以做到这一点。我都试过了,两个选项都很好。我现在和第一个一起去了,这真的没关系。这是我在 Startup.Auth.cs 文件中的 cmets。

// 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"
});

【讨论】:

    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    相关资源
    最近更新 更多