Skip to content

Instantly share code, notes, and snippets.

@allenmichael
Last active November 11, 2017 00:21
Show Gist options
  • Save allenmichael/e024f50afef0148bb3d914c685208cf5 to your computer and use it in GitHub Desktop.
Save allenmichael/e024f50afef0148bb3d914c685208cf5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BoxOAuth.Models;
using Microsoft.Extensions.Options;
using Box.V2.Config;
using Box.V2;
using Box.V2.Auth;
namespace BoxOAuth.Controllers
{
public class OAuthController : Controller
{
const string BOX_AUTH_URL = "https://account.box.com/api/oauth2/authorize";
private BoxAppSettings _boxSettings { get; set; }
private BoxClient _boxClient { get; set; }
private BoxConfig _boxConfig { get; set; }
public OAuthSession BoxAuthenicatedSession { get; set; }
public OAuthController(IOptions<BoxAppSettings> settings)
{
this._boxSettings = settings.Value;
this._boxConfig = new BoxConfig(this._boxSettings.ClientId,
this._boxSettings.ClientSecret,
new Uri(this._boxSettings.RedirectUrl));
this._boxClient = new BoxClient(this._boxConfig);
}
[Route("start")]
public IActionResult Start()
{
var clientIdParam = $"client_id={this._boxSettings.ClientId}";
var redirectUrlParam = $"redirect_uri={this._boxSettings.RedirectUrl}";
return Redirect(
$"{BOX_AUTH_URL}?response_type=code&{clientIdParam}&{redirectUrlParam}");
}
[Route("return")]
public async Task<IActionResult> Return()
{
var code = Request.Query["code"];
this.BoxAuthenicatedSession = await this._boxClient.Auth.AuthenticateAsync(code);
var client = new BoxClient(this._boxConfig, this.BoxAuthenicatedSession);
var user = await client.UsersManager.GetCurrentUserInformationAsync();
System.Console.WriteLine(user.Name);
return Content($"Hello, {user.Name}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment