this shouldn't be a POST

This commit is contained in:
Michael 2017-04-30 10:55:47 -04:00
parent 3d7126efd7
commit c9716799cc
2 changed files with 88 additions and 3 deletions

View file

@ -1,17 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Project_Unite.Models;
namespace Project_Unite.Controllers
{
public class OAuth2Controller : Controller
{
// GET: OAuth2
public ActionResult Index()
private ApplicationSignInManager _signInManager = null;
private ApplicationUserManager _userManager = null;
public ApplicationSignInManager SignInManager
{
return View();
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
[AllowAnonymous]
public async Task<ActionResult> Login(string appname, string appdesc, string version)
{
try
{
string authHeader = Request.Headers["Authentication"];
string b64_auth = authHeader.Remove(0, 6); //get rid of the "Basic " text.
byte[] data = Convert.FromBase64String(b64_auth);
string plaintext = Encoding.UTF8.GetString(data);
string[] split = plaintext.Split(':');
string username = split[0];
string password = split[1];
var result = await SignInManager.PasswordSignInAsync(username, password, false, false);
if(result == Microsoft.AspNet.Identity.Owin.SignInStatus.Success)
{
var db = new ApplicationDbContext();
var user = db.Users.FirstOrDefault(x => x.UserName == username);
var auth_token = db.OAuthTokens.Where(x => x.UserId == user.Id).FirstOrDefault(x => x.AppName == appname && x.AppDescription == appdesc && x.Version == version);
if(auth_token == null)
{
auth_token = new Models.OAuthToken
{
Id = Guid.NewGuid().ToString(),
UserId = user.Id,
AppName = appname,
AppDescription = appdesc,
Version = version
};
db.OAuthTokens.Add(auth_token);
db.SaveChanges();
return Content(auth_token.Id);
}
else
{
return Content(auth_token.Id);
}
}
else
{
return new HttpStatusCodeResult(403);
}
}
catch
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
}
}
}

View file

@ -247,6 +247,16 @@ public static ApplicationDbContext Create()
public DbSet<ForumPost> ForumPosts { get; set; }
public DbSet<Story> Stories { get; set; }
public DbSet<View> Views { get; set; }
public DbSet<OAuthToken> OAuthTokens { get; set; }
}
public class OAuthToken
{
public string Id { get; set; }
public string UserId { get; set; }
public string AppName { get; set; }
public string AppDescription { get; set; }
public string Version { get; set; }
}
public class ReadPost