public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); var db = new RoomMeDataContext(); var store = new UserStore <User>(db); using (var manager = new UserManager <User>(store)) { var user = manager.Find(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "Incorrect username or password"); return; } } var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName)); identity.AddClaim(new Claim(ClaimTypes.Role, "user")); context.Validated(identity); }
public UsersController() { db = new RoomMeDataContext(); var store = new UserStore <User>(db); _userManager = new UserManager <User>(store); }
protected override void Seed(RoomMe.Api.Infrastructure.RoomMeDataContext context) { #region Generate random users if (context.Users.Count() == 0) { var store = new UserStore <User>(context); using (var manager = new UserManager <User>(store)) { for (int i = 0; i < 100; i++) { string randomEmailAddress = Faker.Internet.Email(); var user = new User { Id = Guid.NewGuid().ToString(), UserName = randomEmailAddress, FirstName = Faker.Name.First(), LastName = Faker.Name.Last(), Email = randomEmailAddress, PhoneNumber = Faker.Phone.Number() }; manager.Create(user, "password123"); } } } #endregion context = new RoomMeDataContext(); #region Generate random listings foreach (var user in context.Users) { int numberOfListings = Faker.RandomNumber.Next(0, 5); for (int i = 0; i < numberOfListings; i++) { var listing = new Listing { UserId = user.Id, Address = Faker.Address.StreetAddress(), City = Faker.Address.City(), State = Faker.Address.UsState(), Zip = Faker.Address.ZipCode(), Description = Faker.Company.BS(), Price = Faker.RandomNumber.Next(100, 10000), Latitude = (float)new Bogus.Faker().Address.Latitude(), Longitude = (float)new Bogus.Faker().Address.Longitude() }; context.Listings.Add(listing); } } context.SaveChanges(); #endregion #region Generate random photos for listings foreach (var listing in context.Listings) { int numberOfPhotos = Faker.RandomNumber.Next(0, 5); for (int j = 0; j < numberOfPhotos; j++) { context.ListingPhotoes.Add(new ListingPhoto { ListingId = listing.ListingId, Title = Faker.Company.BS(), Url = new Bogus.Faker().Image.City(randomize: false) }); } } context.SaveChanges(); #endregion }