使用asp.net mvc

时间:2015-10-14 11:00:38

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

我试图在ASP.NET MVC 5中使用oauth。

在谷歌的开发者控制台中,我提出了重定向uri:

www.mydomain.com/account/externallogincallback

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

我说:

www.mydomain.com/signin-google

它有效!

我试图搜索字符串" signin-google"在我的项目中但无法在任何地方找到它。

有人可以告诉我发生了什么事吗?为什么会这样?感谢。

1 个答案:

答案 0 :(得分:7)

我懒得写一个格式正确的答案,我把这些评论放在代码中让自己记住如何解决这个问题。这不是一个真正的问题,只是我从不打扰正确阅读的东西:)但这是你可以做的使它工作。有两种选择你可以做到这一点。我试过了两个,两个选项都运行得很好。我现在和第一个一起去,这没关系。以下是我在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"
});