使用asp.net MVC与Google重定向URI [英] Redirect URI with Google using asp.net MVC

查看:88
本文介绍了使用asp.net MVC与Google重定向URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ASP.NET MVC 5中将oauth与Google结合使用.

I'm trying to use oauth with Google in ASP.NET MVC 5.

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

In Google's developer console I put for the redirect uri:

www.mydomain.com/account/externallogincallback

并认为这会做到.但事实并非如此.

and thought that this will do. But it didn't.

我把:

www.mydomain.com/signin-google

成功了!

我试图在项目中搜索字符串"signin-google",但找不到任何地方.

I tried to search the string "signin-google" in my project but couldn't find it anywhere.

有人可以告诉我怎么回事吗?为什么?谢谢.

Can someone tell me what is going on? why is that so? thanks.

推荐答案

我懒得写一个格式正确的答案,我将这些注释放在代码中,以备不时之需.这并不是真正的问题,只是我从不费心阅读的内容:)但这是使它正常运行的方法.有2种选择方式.我都尝试过,两个选项都很好.我现在和第一个一起去了,真的没关系.这是我在Startup.Auth.cs文件中的评论.

I am too lazy to write a properly formatted answer, I placed these comments in code for myself to remember how to resolve this issue. It is not really an issue, just something I never bothered to read properly :) But this is what you can do to make it work. There 2 options how you can do it. I have tried both and both options work just fine. I went with the first one for now, it really doesnt matter. Here are my comments in Startup.Auth.cs file.

// 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与Google重定向URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆