Added Bearer oAuth Authentication:
* Create User with roles * Login * Authorization without roles
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Web.Helpers;
|
||||
using Step.Model;
|
||||
|
||||
namespace Step.Database.Controllers
|
||||
{
|
||||
public class UsersController : IDisposable
|
||||
{
|
||||
private DatabaseContext dbCtx;
|
||||
|
||||
public UsersController()
|
||||
{
|
||||
// Initialize database context
|
||||
dbCtx = new DatabaseContext();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Clear database context
|
||||
dbCtx.Dispose();
|
||||
}
|
||||
|
||||
public void Create(string userName, string password, string firstName, string lastName, int roleId)
|
||||
{
|
||||
// Create a new user model with params
|
||||
UserModel user = new UserModel()
|
||||
{
|
||||
Username = userName,
|
||||
Password = Crypto.HashPassword(password),
|
||||
FirstName = firstName,
|
||||
LastName = lastName,
|
||||
RoleId = roleId,
|
||||
SecurityStamp = Guid.NewGuid().ToString()
|
||||
};
|
||||
// Add to database
|
||||
dbCtx.Users.Add(user);
|
||||
// Commit changes
|
||||
dbCtx.SaveChanges();
|
||||
}
|
||||
|
||||
public UserModel Find(int id)
|
||||
{
|
||||
// Find user by Id with Role object included
|
||||
return dbCtx.Users.Include("Role").Where(u => u.UserId == id).First();
|
||||
}
|
||||
|
||||
public UserModel Find(string username)
|
||||
{
|
||||
// Find user by Id with Role object included
|
||||
return dbCtx.Users.Include("Role").Where(u => u.Username == username).FirstOrDefault();
|
||||
}
|
||||
|
||||
public UserModel Find(string username, string password)
|
||||
{
|
||||
// Find if username exists
|
||||
UserModel user = Find(username);
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
// Check if the passwords match
|
||||
if (Crypto.VerifyHashedPassword(user.Password, password) != true)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user