Google Authorization in ASP.Net MVC 6

Michał Smyk
Jan 3, 2016 · 3 min read

So, as the new year break is coming to an end, I finally found some time to look into new ASP.Net 5 MVC version (currently marked as a Release Candidate) and started working on some simple project that I currently call FoodyRater on Visual Studio 2015 Community version. First things that I decided to set up was website layout and authorization by Google account.

As for layout, it went surprisingly smooth — I just right-clicked project name, selected “Managed Bower Packages” and replaced Bootstrap/jQuery with ‘Admin-lte’ package. Then all I had to do was change the shared _Layout.cshtml file and everything started working.

Also at last Visual Studio is offering rather good support for Github repositories. I will have to test how it works with private git repos in a future, but at the moment I am satisfied with what I have.

Next part — Google authorization. First result in Google offered me a rather good tutorial, but of course for previous version of MVC. But still it had some important things that I should remember about, such as setting SSL connection. It was rather easy — I had to get into project properties (ALT+Enter shortcut) and change checkbox “Enable SSL” in Debug tab. After this I changed “App URL” to new one using HTTPS and hell broke loose. “An error occured attempting to determine the process id of the DNX process hosting your application” window showed up. As it always is with Microsoft software, no specific information what actually went wrong is displayed. Googling for a while also resulted with no solutions for my problem, so I had to just try some things on my own. As it turns out, “App Url” field has to reflect to the old (based on HTTP, not HTTPS) Url, and new url should be typed in “Launch Url” field at the top of the page. Oh, and if you for some reason unmark the “Enable X Authentication” checkboxes it won’t work as well.

Second part of setting HTTPS was to add new filter to Startup.cs (which now has most of functionality that was earlies in Global.asax.cs file, and some new features as well.). There I found the following code:

public void ConfigureServices(IServiceCollection services) { (…) services.AddMvc(); (…) }public void ConfigureServices(IServiceCollection services)

As it turns out I just had to add new parameter to AddMvc() method:

services.AddMvc(options => { options.Filters.Add(new Microsoft.AspNet.Mvc.RequireHttpsAttribute()); });services.AddMvc(options =>options.Filters.Add(new Microsoft.AspNet.Mvc.RequireHttpsAttribute());

As for adding authorization by Google, there was a little issue — I could not find any website which would offer me good instruction how to do it properly. Installing NuGet packages with OWIN and Google authorization is not a good idea, as DNX doesn’t support current version of OWIN. Solution was rather easy — I just had to add “Microsoft.AspNet.Authentication.Google”: “1.0.0-rc1-final”, to ‘dependencies’ in project.json file. Then again in Startup.cs add following code:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
(...)
app.UseIdentity();
app.UseGoogleAuthentication(options =>
{
options.ClientId = Configuration["Authentication:Google:ClientId"];
options.ClientSecret = Configuration["Authentication:Google:Secret"];
});
(...)
}

app.UseIdentity() is in this file by default, but it is necessary for the next method to work. Official ASP.Net guide instructs us to install SecretManager, but it is not necessary in VS 2015. You just have to right-click on your project and select option “Manage user secrets” There you have another JSON file, where you add your secrets like this:

{ 
“Authentication:Google:ClientId”: “someId”,
“Authentication:Google:Secret”: “someSecret”
}

Naming convention for those attributes is probably not specified, but I just changed the example provided by guide mentioned before to keep it similar. And it works.

As for me those rather simple tasks took about one hours to comprehend, and I suspect more troubles will start to emerge as development of this project will go on. But that’s probably the price of progress, and if thanks to that developing will be easier for me then I am up to some pain at the beginning for the greater good.


Originally published at www.blog.smyk.it on January 3, 2016.

Smyk.IT

Blog of Software Engineer from Wrocław, Poland

Michał Smyk

Written by

.NET Software developer from Poland.

Smyk.IT

Smyk.IT

Blog of Software Engineer from Wrocław, Poland

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade