Matt
June 28, 2019, 12:33pm
1
I’m trying to get google auth running with jwt. I am currently getting the following error:
“405 - redirected from authenticate from origin ‘null’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ACCESS-CONTROL-ALLOW-ORIGIN is present on the requested resource”
I am researching the error, but wondered if I’m missing an easy config on the auth provider that would save me some pain.
the CORS feature is enabled on the webservice.
mythz
June 28, 2019, 2:12pm
2
Please provide the full HTTP Request/Response errors for the handler + your CorsFeature
configuration.
Matt
June 29, 2019, 11:36am
3
Cors Feature configuration is
host.Plugins.Add(new CorsFeature(
allowedOrigins: "*",
allowedMethods: "GET, POST, PUT, DELETE, OPTIONS",
allowedHeaders: "Origin, X-Requested-With, Content-Type, Accept, Authorization",
allowCredentials: false));`
mythz
June 29, 2019, 6:36pm
4
You should not be initiating the OAuth flow via Ajax, it needs to be a full redirect to the /auth/{provider}
endpoint. You have something that’s forcing the OAuth request via Ajax which wont work.
Matt
June 30, 2019, 6:05am
5
Thanks mythz, will pass this info to the web developer.
Matt
July 1, 2019, 2:05pm
6
There was indeed an issue with the ajax call, however also had found that the following code was required to initialize the google auth settings:
public CustomGoogleAuthProvider(IAppSettings appSettings) : base(appSettings)
{
this.ConsumerKey = appSettings.GetString("AppSettings:oauth.google.ConsumerKey");
this.ConsumerSecret = appSettings.GetString("AppSettings:oauth.google.ConsumerSecret");
this.RedirectUrl = appSettings.GetString("AppSettings:oauth.google.RedirectUrl");
this.CallbackUrl = appSettings.GetString("AppSettings:oauth.google.CallbackUrl");
}
otherwise they were not initialized.
This is using dotnet core.
mythz
July 1, 2019, 6:06pm
7
The OAuth keys for registering OAuth providers is just "oauth.google.*"
, e.g:
"oauth.RedirectUrl": "https://localhost:5001/",
"oauth.CallbackUrl": "https://localhost:5001/auth/{0}",
"oauth.facebook.Permissions": [ "email", "user_location" ],
"oauth.facebook.AppId": "531608123577340",
"oauth.facebook.AppSecret": "9e1e6591a7f15cbc1b305729f4b14c0b",
"oauth.google.ConsumerKey": "274592649256-nmvuiu5ri7s1nghilbo6nmfd6h8j71sc.apps.googleusercontent.com",
"oauth.google.ConsumerSecret": "aKOJngvq0USp3kyA_mkFH8Il",
"oauth.microsoftgraph.AppId": "8208d98e-400d-4ce9-89ba-d92610c67e13",
"oauth.microsoftgraph.AppSecret": "hsrMP46|_kfkcYCWSW516?%",
"oauth.microsoftgraph.SavePhoto": "true",
"oauth.microsoftgraph.SavePhotoSize": "64x64"
Matt
July 1, 2019, 6:08pm
8
Yeah that’s what I have above right ? but they were not initializing, I had to reinitialize them
mythz
July 1, 2019, 7:35pm
9
No, if you used the correct keys you wouldn’t need to manually configure them, it would be configured when using the AppSettings, e.g:
new GoogleAuthProvider(AppSettings),
There’s never any AppSettings:
prefix in any config.
Matt
July 1, 2019, 8:44pm
10
My mistake, I was still on .Net structure and was expecting AppSettings section. AppSettings structure in ,json and in .config are different
e.g.
<appSettings>
<add key="Service.Account.URL" value="http://localhost:9000/" />
<add key="Service.Activity.URL" value="http://localhost:9001/" />
Client = new JsonServiceClient(ConfigurationManager.AppSettings["Service.Account.URL"])
vs
"AppSettings": {
"Service.Account.URL": "http://localhost:9000/",
"Service.Connect.URL": "http://localhost:9004/",
JsonServiceClient(ServiceStack.HostContext.AppSettings.GetString("AppSettings:Service.Account.URL"))
mythz
July 1, 2019, 8:55pm
11
There still should never be any AppSettings:
prefix for any reason, If you’re using .NET Framework your OAuth keys are exactly the same :
<appSettings>
<add key="oauth.RedirectUrl" value="https://yourhostname.com"/>
<add key="oauth.CallbackUrl" value="https://yourhostname.com/auth/{0}"/>
<add key="oauth.twitter.ConsumerKey" value="3H1FHjGbA1N0n0aT5yApA"/>
<add key="oauth.twitter.ConsumerSecret" value="MLrZ0ujK6DwyjlRk2YLp6HwSdoBjtuqwXeHDQLv0Q"/>
</appSettings>
Which is also what you should use when accessing it via AppSettings :
var url = AppSettings.GetString("Service.Account.URL");
Matt
July 1, 2019, 8:57pm
12
Yes of course I get your point but nevertheless there is a difference in how .Net treats “AppSettings” to how .Net Core does.
The keys as you say are the same, but the structure is different, and in .Net Core, the structure is part of the key retrieval
mythz
July 1, 2019, 9:00pm
13
You would still use exactly the same code to access the key in both:
var url = AppSettings.GetString("Service.Account.URL");
In .NET Core the :
prefix as special meaning to traverse object graphs which isn’t used for accessing simple keys like the OAuth configuration. Regardless there should never be an AppSettings:
prefix unless you’re putting the configuration inside your own custom "AppSettings"
object graph which you shouldn’t be doing.
Matt
July 1, 2019, 9:02pm
14
We are talking at cross purposes Mythz, I’m agreeing with you.
I was merely making an observation about the structural differences which can be confusing:
Matt
July 1, 2019, 9:04pm
15
I’m just explaining where the confusion came from.
mythz
July 1, 2019, 9:04pm
16
Do not invent your own AppSettings object graph:
"AppSettings": { ... }
Your whole appsettings.json
is the configuration, add them as simple properties:
{
"Service.Account.URL": "..."
}
Then both will be accessible using the exact same code:
var url = AppSettings.GetString("Service.Account.URL");
1 Like
Matt
July 1, 2019, 9:30pm
18
What is the correct way to get the JWT once google authentication has completed ?
mythz
July 1, 2019, 9:37pm
19
1 Like
Matt
July 1, 2019, 9:40pm
20
Awesome Mythz thanks as always!