Skip to content

Instantly share code, notes, and snippets.

@ivanvs
Created January 23, 2019 23:01
Show Gist options
  • Select an option

  • Save ivanvs/66d1dddc4138e8b0e157c1dee3a99a7f to your computer and use it in GitHub Desktop.

Select an option

Save ivanvs/66d1dddc4138e8b0e157c1dee3a99a7f to your computer and use it in GitHub Desktop.
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using TestWebApp.Infrastructure;
using TestWebApp.Models;
using TestWebApp.Repositories;
namespace TestWebApp.Models
{
public class DataAccessContext : IdentityDbContext<User>
{
//ovo bi trebao da je AuthContex, ali Marko kaze da je svejedno kako se zove...
//moguce da treba drugacije da izgleda, mozda bazu nisam dobro napravila, ali mi ona inace uvek radi
public DataAccessContext() : base("DataAccessConnection")
{
Database.SetInitializer(new InitializeWithDefaultData());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// must define for db relations
modelBuilder.Entity<Student>().ToTable("Student");
modelBuilder.Entity<Admin>().ToTable("Admin");
modelBuilder.Entity<Student>().HasRequired(s => s.User).WithOptional(u => u.Student);
modelBuilder.Entity<Grade>().HasRequired(g => g.Student).WithMany(s => s.Grades);
}
}
}
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using TestWebApp.Models;
namespace TestWebApp.Infrastructure
{
public class InitializeWithDefaultData : DropCreateDatabaseAlways<DataAccessContext>
{
protected override void Seed(DataAccessContext context)
{
using (var store = new RoleStore<IdentityRole>(context))
{
using (var manager = new RoleManager<IdentityRole>(store))
{
manager.Create(new IdentityRole("admin"));
manager.Create(new IdentityRole("student"));
manager.Create(new IdentityRole("parent"));
manager.Create(new IdentityRole("teacher"));
}
}
context.SaveChanges();
}
}
}
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security.OAuth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using TestWebApp.Models;
using TestWebApp.Repositories;
using Unity;
namespace TestWebApp.Providers
{
public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
private UnityContainer container;
public SimpleAuthorizationServerProvider(UnityContainer container)
{
this.container = container;
}
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
User user = null;
IEnumerable<string> roles = null;
IAuthRepository _repo = container.Resolve<IAuthRepository>();
user = await _repo.FindUser(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
roles = await _repo.FindRoles(user.Id);
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
//identity.AddClaim(new Claim(ClaimTypes.Email, context.UserName));
identity.AddClaim(new Claim(ClaimTypes.Name, ((User)user)?.FirstName));
//identity.AddClaim(new Claim(ClaimTypes.Surname, ((ApplicationUser)user)?.LastName));
identity.AddClaim(new Claim(ClaimTypes.Role, string.Join(",", roles)));
identity.AddClaim(new Claim("UserId", user.Id));
context.Validated(identity);
_repo.Dispose();
}
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
Dictionary<string, string> additionalUserInfo = new Dictionary<string, string>();
foreach (Claim claim in context.Identity.Claims)
{
context.AdditionalResponseParameters.Add(claim.Type.Split('/').Last(), claim.Value);
}
foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
{
context.AdditionalResponseParameters.Add(property.Key, property.Value);
}
return Task.FromResult<object>(null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment