Ejemplo n.º 1
0
        public async Task<IdentityResult> RegisterUser(UserLogin login)
        {
            IdentityResult result = null;
            User user = new User()
            {
                UserName = login.Username,
                Email = login.Email,
                Image = login.Image,
                PositionUser = login.PositionUser,
                LastLogin = DateTimeOffset.Now              
            };
            
            try
            {
                result = await _userManager.CreateAsync(user, login.Password);                
                if (result.Succeeded)
                {
                    string token = await _userManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    string encode = HttpServerUtility.UrlTokenEncode(Encoding.UTF8.GetBytes(token));
                    // Settare il callback url che abilita l'utente.
                    Uri callbackUrl = new Uri(String.Format("{0}/mail-activation.html?userId={1}&token={2}", HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.AbsolutePath.ToString(), ""), user.Id, encode));

                    // Caricare il testo della mail e riempire i dati
                    string mailText = File.ReadAllText(HttpContext.Current.Server.MapPath("~/MailTemplate/template.html"));
                    mailText = mailText.Replace("{URL}", callbackUrl.ToString());
                    mailText = mailText.Replace("{USER}", user.UserName);
                    mailText = mailText.Replace("{USERPIC}", Convert.ToBase64String(user.Image));                    

                    await _userManager.SendEmailAsync(user.Id,"UplayAgain ti da il benvenuto. Conferma la tua password!", mailText);
                }
            }
            catch (DbEntityValidationException ex)
            {
                StringBuilder sb = new StringBuilder();
                ex.EntityValidationErrors.ToList().ForEach(entityValidation => { entityValidation.ValidationErrors.ToList().ForEach(validation => sb.Append(string.Concat(validation.PropertyName, " - ", validation.ErrorMessage))); });

                _log.Error("{0}{1}Validation errors:{1}{2}", ex, Environment.NewLine, sb.ToString());
                throw;
            }
            catch (Exception ex)
            {
                _log.Error(ex);
            }
            return result;
        }
Ejemplo n.º 2
0
 public async Task<IHttpActionResult> Register(UserLogin userModel) // UserLogin
 {
     try
     {
         IdentityResult result = await _repo.RegisterUser(userModel);
         IHttpActionResult errorResult = GetErrorResult(result);
         if (errorResult != null)
         {
             return errorResult;
         }
         else
             return Ok();
     }
     catch(Exception ex)
     {
         _log.Error(ex);
         return BadRequest("Errore" + ex.Message);
     }                
 }