示例#1
0
 public async Task AddCurrentAppIdToUserIfNotAlreadyAdded(AppMember user, string appId)
 {
     if (user != null && !await _userStore.IsUserRegisteredForAppAsync(user, appId)) // user == null on registration
     {
         await _userStore.AddAppToUserAsync(user, appId);
     }
 }
示例#2
0
 public async Task AddCurrentServiceIdToUserIfNotAlreadyAdded(AppMember user, string serviceId)
 {
     if (user != null && !await _userStore.IsUserRegisteredForServiceAsync(user, serviceId)) // user == null on registration
     {
         await _userStore.AddServiceToUserAsync(user, serviceId);
     }
 }
示例#3
0
 private async Task<AppMember> CreateExternalUser(string email, ExternalLoginInfo info)
 {
     var user = new AppMember {UserName = email, Email = email};
     var result = await UserManager.CreateAsync(user);
     if (result.Succeeded)
     {
         result = await UserManager.AddLoginAsync(user.Id, info.Login);
         if (result.Succeeded)
         {
             return await Task.FromResult(user);
         }
         else
         {
             Logger.Trace("Create user failed: {0}", string.Join("; ", result.Errors));
             AddErrors(result);
         }
     }
     else
     {
         Logger.Trace("Create user failed: {0}", string.Join("; ", result.Errors));
         AddErrors(result);
     }
     return await Task.FromResult<AppMember>(null);
 }
示例#4
0
 private async Task AddLoginToUser(AppMember user, ExternalLoginInfo info)
 {
     if (!(await _userStore.GetLoginsAsync(user)).Any(x =>
         info.Login.LoginProvider == x.LoginProvider &&
         info.Login.ProviderKey == x.ProviderKey))
     {
         await _userStore.AddLoginAsync(user, info.Login);
     }
 }
示例#5
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new AppMember { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    //  Comment the following line to prevent log in until the user is confirmed.
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    var returnUrl = Request.QueryString["ReturnUrl"];
                    return string.IsNullOrEmpty(returnUrl) ? RedirectToAction("Index", "Home") : RedirectToLocal(returnUrl);
                }
                var message = $"Register failed: {string.Join("; ", result.Errors)}";
                StackExchange.Exceptional.ErrorStore.LogException(new Exception(message), null);

                // avoid Name x is already taken. errors
                var identityResult = new IdentityResult(result.Errors.Where(x => !x.StartsWith("Name ")));
                AddErrors(identityResult);

                // Show a hint about external providers
                ModelState.AddModelError("",
                    "You might already have registered using " + WebConfigurationManager.AppSettings["AuthenticationProviders"].ReplaceLast(",", " or ") +
                    ". If this is the case, login with one of those to set a password.");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
示例#6
0
 private async Task<string> GenerateLoginSuggestionMessage(AppMember user)
 {
     var availableLogins = await _userStore.GetLoginsAsync(user);
     if (!availableLogins.Any()) return "";
     return string.Format("Try logging in using {0}{1}.", availableLogins.Count > 1 ? "one of " : "",
             availableLogins.Select(x => x.LoginProvider).Aggregate((x1, x2) => $"{x1}, {x2}"));
 }