public async Task <HttpResponseMessage> Register(RegisterBindingModel model) { List <string> errors = new List <string>(); errors = ModelErrorChecker.Check(ModelState); if (errors.Count == 0) { var user = new DeCamaroong.Domain.User() { UserName = model.Email, Email = model.Email }; if (UserManager.FindByEmail(model.Email) == null) { IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(Request.CreateResponse(HttpStatusCode.NotAcceptable, result.Errors)); } } else { return(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Email address is already in use.")); } } else { return(Request.CreateResponse(HttpStatusCode.NotAcceptable, errors)); } return(Request.CreateResponse(HttpStatusCode.OK)); }
public async Task <IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var info = await Authentication.GetExternalLoginInfoAsync(); if (info == null) { return(InternalServerError()); } var user = new DeCamaroong.Domain.User() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user); if (!result.Succeeded) { return(GetErrorResult(result)); } result = await UserManager.AddLoginAsync(user.Id, info.Login); if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }
public async Task <IHttpActionResult> GetExternalLogin(string provider, string error = null) { if (error != null) { return(Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error))); } if (!User.Identity.IsAuthenticated) { return(new ChallengeResult(provider, this)); } ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity); if (externalLogin == null) { return(InternalServerError()); } if (externalLogin.LoginProvider != provider) { Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); return(new ChallengeResult(provider, this)); } DeCamaroong.Domain.User user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider, externalLogin.ProviderKey)); bool hasRegistered = user != null; if (hasRegistered) { Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager, OAuthDefaults.AuthenticationType); ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName); Authentication.SignIn(properties, oAuthIdentity, cookieIdentity); } else { IEnumerable <Claim> claims = externalLogin.GetClaims(); ClaimsIdentity identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType); Authentication.SignIn(identity); } return(Ok()); }
public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var info = await Authentication.GetExternalLoginInfoAsync(); if (info == null) { return InternalServerError(); } var user = new DeCamaroong.Domain.User() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user); if (!result.Succeeded) { return GetErrorResult(result); } result = await UserManager.AddLoginAsync(user.Id, info.Login); if (!result.Succeeded) { return GetErrorResult(result); } return Ok(); }
public async Task<HttpResponseMessage> Register(RegisterBindingModel model) { List<string> errors = new List<string>(); errors = ModelErrorChecker.Check(ModelState); if (errors.Count == 0) { var user = new DeCamaroong.Domain.User() { UserName = model.Email, Email = model.Email }; if (UserManager.FindByEmail(model.Email) == null) { IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return Request.CreateResponse(HttpStatusCode.NotAcceptable, result.Errors); } } else return Request.CreateResponse(HttpStatusCode.NotAcceptable, "Email address is already in use."); } else return Request.CreateResponse(HttpStatusCode.NotAcceptable, errors); return Request.CreateResponse(HttpStatusCode.OK); }