简体   繁体   中英

Where to set the OAuth redirect_uri in ASP.NET 4.5 webforms application

I can't find anywhere how to set the OAuth2 redirect_uri in an ASP.NET 4.5 webforms application. By default it's set to localhost and of course I get this error from google:

The redirect URI in the request: http://localhost:3884/signin-google did not 
match a registered redirect URI

And this from Facebook:

Given URL is not allowed by the Application configuration.: One or more of the
given URLs is not allowed by the App's settings. It must match the Website URL
or Canvas URL, or the domain must be a subdomain of one of the App's domains.

And I get this error from my website domain (not from the localhost).

I was testing the built in external login logic for authentication, and I got this error even though my Google API credentials were correct. For example the redirect URI was set to:

http://localhost:29405/signin-google

The odd thing was that the default CallbackPath for the Google Authentication is "/signin-google" but I had to set this anyway in App_Start/Startup.Auth, so I added this line and it worked:

so...

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
        {
            ClientId = "YOUR CLIENT ID",
            ClientSecret = "YOUR CLIENT SECRET",
            CallbackPath = new PathString("/signin-google")
        });

After looking at a lot of answers this is what worked for me:

  1. Enable Google + API
  2. The default redirect URI is /signin-google, so add this to the Authorized redirect URI. 在此输入图像描述

  3. Add this to the RouteConfig File:

    routes.MapRoute(name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "ExternalLoginCallback" });

Hope it helps anyone that cross by this problem.

You have to enable Google+ API (it's not enabled by default).

启用Google+ API https://console.developers.google.com/project/ projectId /apiui/apiview/plus

Default CallbackPath ( /signin-google ) is ok, but you have to add it to REDIRECT URIS at Credentials - OAuth ClientID (you can replace the default /oauth2callback ).

Also need to turn the SSL ON

I did what thenninger and Sanchitos posted.

But only after i run my webApp with SSL then it worked.

You can turn on SSL in project properties here:

在此输入图像描述

And make sure application starts on correct address:

在此输入图像描述

我需要在路径中添加更多路径,如下所示:

 routes.MapRoute(name: "signin-google", url: "localhost:44336/signin-google", defaults: new { controller = "Account", action = "ExternalLoginCallback" });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM