public SimpleMembershipInitializer() { Database.SetInitializer<UsersContext>(null); try { using (var context = new UsersContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } }
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Insert a new user into the database using (UsersContext db = new UsersContext()) { UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table UserProfile newUser = db.UserProfiles.Add(new UserProfile { UserName = model.UserName }); db.SaveChanges(); db.ExternalUsers.Add(new ExternalUserInformation { UserId = newUser.UserId, login = model.login, id = model.id, avatar_url = model.avatar_url, gravatar_id = model.gravatar_id, url = model.url, html_url = model.html_url, followers_url = model.followers_url, following_url = model.following_url, gists_url = model.gists_url, starred_url = model.starred_url, subscriptions_url = model.subscriptions_url, organizations_url = model.organizations_url, repos_url = model.repos_url, events_url = model.events_url, received_events_url = model.received_events_url, type = model.type, site_admin = model.site_admin }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name."); } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); }
private bool ValidateUser() { try { UsersContext user_context = new UsersContext(); MembershipUser member = Membership.GetUser(); int UserID = Int32.Parse(member.ProviderUserKey.ToString()); //UserProfile user = user_context.UserProfiles.Where(d => d.UserId == UserID).First(); ExternalUserInformation git_info = user_context.ExternalUsers.Where(d => d.UserId == UserID).First(); RestClient client = new RestClient(clientURL); client.Authenticator = new HttpBasicAuthenticator(username, password); RestRequest request = new RestRequest("orgs/{org}/members/{user}", Method.GET); request.AddUrlSegment("org", org); request.AddUrlSegment("user", git_info.login); var temp = client.Execute(request); if (temp.StatusCode == System.Net.HttpStatusCode.NoContent) { return true; } return false; } catch(Exception ex) { return false; } }