// POST api/CustomRegistration public HttpResponseMessage Post(RegistrationRequest registrationRequest) { if (!Regex.IsMatch(registrationRequest.username, "^[a-zA-Z0-9]{4,}$")) { return this.Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid username (at least 4 chars, alphanumeric only)"); } else if (registrationRequest.password.Length < 8) { return this.Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid password (at least 8 chars required)"); } MobileServiceContext context = new MobileServiceContext(); Account account; try { account = context.Accounts.Where(a => a.Username == registrationRequest.username).SingleOrDefault(); } catch(Exception e) { Console.WriteLine(e.StackTrace); return this.Request.CreateResponse(HttpStatusCode.InternalServerError); } if (account != null) { return this.Request.CreateResponse(HttpStatusCode.BadRequest, "That username already exists."); } else { byte[] salt = LoginProviderUtil.generateSalt(); Account newAccount = new Account { Id = Guid.NewGuid().ToString(), Username = registrationRequest.username, Salt = salt, SaltedAndHashedPassword = LoginProviderUtil.hash(registrationRequest.password, salt) }; context.Accounts.Add(newAccount); context.SaveChanges(); UserInfo newUserInfo; context.UserInfos.Add(newUserInfo = new UserInfo() { Bio = "About me", UserId = newAccount.Id, Id = Guid.NewGuid().ToString() }); context.SaveChanges(); return this.Request.CreateResponse(HttpStatusCode.Created); } }
// POST api/FBConnectUser public async System.Threading.Tasks.Task<HttpResponseMessage> Get(NetworkType providerType) { ServiceUser user = this.User as ServiceUser; HttpStatusCode httpStatus = HttpStatusCode.OK; MobileServiceContext context = new MobileServiceContext(); Account account = null; ProviderCredentials creds = null; bool firstLogin = false; if (providerType == NetworkType.FACEBOOK) { creds = (await user.GetIdentitiesAsync()).OfType<FacebookCredentials>().FirstOrDefault(); if (creds != null) { account = context.Accounts.FirstOrDefault(a => a.FacebookId == creds.UserId); if(account == null) { firstLogin = true; account = new Account() { Id = Guid.NewGuid().ToString(), Username = "******", FacebookId = creds.UserId }; context.Accounts.Add(account); UserInfo newUserInfo; context.UserInfos.Add(newUserInfo = new UserInfo() { UserId = account.Id, Id = Guid.NewGuid().ToString() }); context.SaveChanges(); httpStatus = HttpStatusCode.Created; } } } if (providerType == NetworkType.LINKED_IN) { creds = (await user.GetIdentitiesAsync()).OfType<LinkedInCredentials>().FirstOrDefault(); if (creds != null) { account = context.Accounts.FirstOrDefault(a => a.LinkedInId == creds.UserId); if (account == null) { firstLogin = true; account = new Account() { Id = Guid.NewGuid().ToString(), Username = "******", LinkedInId = creds.UserId }; context.Accounts.Add(account); UserInfo newUserInfo; context.UserInfos.Add(newUserInfo = new UserInfo() { UserId = account.Id, Id = Guid.NewGuid().ToString() }); context.SaveChanges(); httpStatus = HttpStatusCode.Created; } } } if (account != null) { var customLoginResult = new ProviderLoginResult() { AccountId = account.Id, username = account.Username, FirstLogin = firstLogin }; return this.Request.CreateResponse(httpStatus, customLoginResult); } return this.Request.CreateResponse(httpStatus); }
public bool Equals(Account x, Account y) { return x.Id == y.Id; }