public async Task <IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model) { if (model == null) { return(BadRequest(ModelState)); } var verifiedAccessToken = await VerifyExternalAccessToken(model.Provider, model.ExternalAccessToken); if (verifiedAccessToken == null) { return(BadRequest("Invalid Provider or External Access Token")); } IdentityUser user = await _applicationUser.FindAsync(new UserLoginInfo(model.Provider, verifiedAccessToken.user_id)); var hasRegistered = user != null; if (hasRegistered) { return(BadRequest("External user is already registered")); } var command = new IdentityUserApplication(model.Email, model.UserName); var created = await _applicationUser.CreateAsync(command); if (!created.Succeeded) { return(GetErrorResult(created)); } var info = new ExternalLoginInfo() { DefaultUserName = model.UserName, Login = new UserLoginInfo(model.Provider, verifiedAccessToken.user_id) }; user = await _applicationUser.FindByNameAsync(model.UserName); created = await _applicationUser.AddLoginAsync(user.Id, info.Login); if (!created.Succeeded) { return(GetErrorResult(created)); } //generate access token response var accessTokenResponse = GenerateLocalAccessTokenResponse(model.UserName); return(Ok(accessTokenResponse)); }
public async Task <HttpResponseMessage> Post([FromBody] dynamic body) { var command = new IdentityUserApplication((string)body.email, (string)body.userName); var created = await _applicationUser.CreateAsync(command, (string)body.password); if (created.Succeeded) { return(Request.CreateResponse(HttpStatusCode.Created, command)); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest, created.Errors)); } }