public async Task <IActionResult> ConfirmEmailAsync(string userId, string code)
        {
            if (userId == null || code == null)
            {
                return(BadRequest());
            }

            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{userId}'."));
            }

            code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
            var result = await _userManager.ConfirmEmailAsync(user, code);

            StatusMessage = new StatusMessageViewModel(result.Succeeded, result.Succeeded ? "Thank you for confirming your email." : "Error confirming your email.");

            if (result.Succeeded)
            {
                _logger.LogInformation("User '{Email}' confirm the email.", user.Email);
            }

            return(View());
        }
        public async Task <ActionResult> LinkLoginCallbackAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID 'user.Id'."));
            }

            var info = await _signInManager.GetExternalLoginInfoAsync(user.Id);

            if (info == null)
            {
                throw new InvalidOperationException($"Unexpected error occurred loading external login info for user with ID '{user.Id}'.");
            }

            var result = await _userManager.AddLoginAsync(user, info);

            if (!result.Succeeded)
            {
                StatusMessage = StatusMessageViewModel.Error("The external login was not added. External logins can only be associated with one account.");
                return(View("LinkLoginCallbackConfirmation"));
            }

            _logger.LogInformation("User '{Email}' link new login '{LoginProvider}'.", user.Email, info.LoginProvider);

            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            //StatusMessage = "The external login was added.";
            return(Redirect("~/admin#/profile"));
        }
        public async Task <IActionResult> SetPasswordAsync(SetPasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            var addPasswordResult = await _userManager.AddPasswordAsync(user, model.NewPassword);

            if (!addPasswordResult.Succeeded)
            {
                foreach (var error in addPasswordResult.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                return(View());
            }
            else
            {
                _logger.LogInformation("User '{Email}' set password.", user.Email);
            }

            await _signInManager.RefreshSignInAsync(user);

            StatusMessage = StatusMessageViewModel.Succeed("Your password has been set.");

            return(View());
        }
        public async Task <IActionResult> ChangePasswordAsync(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            var changePasswordResult = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword);

            if (!changePasswordResult.Succeeded)
            {
                foreach (var error in changePasswordResult.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                return(View());
            }

            await _signInManager.RefreshSignInAsync(user);

            _logger.LogInformation("User changed their password successfully.");

            StatusMessage = StatusMessageViewModel.Succeed("Your password has been changed.");

            return(View());
        }
示例#5
0
        public async Task <IActionResult> ResetPassword(ResetPasswordRequest request)
        {
            StatusMessageViewModel msg = null;
            var result = await _authService.ResetPasswordAsync(request);

            if (result)
            {
                msg = GetMessage("200", "Your password is reset. Please click to <a href=\"/Account/Login\">login</a");
            }
            return(View("StatusMessage", msg));
        }
示例#6
0
        /// <summary>
        /// userId and code are returned from email active link
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <ActionResult> ConfirmEmail(string userId, string token)
        {
            StatusMessageViewModel message = null;

            var status = await _authService.ConfirmEmailAsync(userId, token);

            if (status)
            {
                message = GetMessage("200", $"Your account is actived successfully. Click <a href='/Account/Login/'> here </a> to login");
            }
            return(View("StatusMessage", message));
        }
        public async Task <IActionResult> Predict(DiabetesViewModel model)
        {
            var lowRisk = await _predictionAPI.GetPrediction(model);

            TempData["_statusMessage"] = new StatusMessageViewModel()
            {
                Message = $"This patient has a {(lowRisk ? "low" : "high")} chance of developing diabetes.",
                Type    = lowRisk ? StatusType.success : StatusType.danger,
                Title   = "Result"
            };

            return(View("Index", model));
        }
        public async Task <IActionResult> ExternalLoginCallbackAsync(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (remoteError != null)
            {
                StatusMessage = StatusMessageViewModel.Error($"Error from external provider: {remoteError}");
                return(RedirectToAction("Login", new { ReturnUrl = returnUrl }));
            }

            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                StatusMessage = StatusMessageViewModel.Error("Error loading external login information.");
                return(RedirectToAction("Login", new { ReturnUrl = returnUrl }));
            }

            // Sign in the user with this external login provider if the user already has a login.
            var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (result.Succeeded)
            {
                _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider);
                return(LocalRedirect(returnUrl));
            }

            if (result.IsLockedOut)
            {
                return(RedirectToAction("Lockout"));
            }
            else
            {
                // If the user does not have an account, then ask the user to create an account.
                var confirmationModel = new ExternalLoginConfirmationViewModel()
                {
                    ReturnUrl     = returnUrl,
                    LoginProvider = info.LoginProvider,
                };

                if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
                {
                    confirmationModel.Email = info.Principal.FindFirstValue(ClaimTypes.Email);
                }

                return(View("ExternalLoginConfirmation", confirmationModel));
            }
        }
示例#9
0
        public async Task <ActionResult> JoinEmbedded(string name, string auth)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(RedirectToAction("Index", "Rooms"));
            }

            // Join
            var room = await db.Rooms.FirstOrDefaultAsync(c => c.Name == name);

            if (room == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // ユーザーを取得できれば取得
            var viewModel = new StatusMessageViewModel <JoinRoomViewModel>()
            {
                Item = new JoinRoomViewModel()
                {
                    Room = room
                }
            };

            var appUser = await GetApplicationUser();

            bool isBroadcaster = (appUser != null && room.Owner.Id == appUser.Id);

            viewModel.Item.IsBroadcaster = isBroadcaster;

            if (!room.IsLive)
            {
                if (appUser == null || !appUser.OwnerRooms.Contains(room))
                {
                    viewModel.Message = "現在ルームはオフラインです。管理者以外は入室できません。";
                    viewModel.Type    = MessageType.Error;
                    return(View(viewModel));
                }
                else if (isBroadcaster)
                {
                    var instance = RoomManager.GetInstance();
                    if (instance.GetRoomInfo(room.Id) == null)
                    {
                        // まだ入れませんよ
                        viewModel.Message = "あなたはこのルームの管理者です。ルームに入室するには、まずエディタからログインしてセッションを確立してください。";
                        viewModel.Type    = MessageType.Error;
                        return(View(viewModel));
                    }
                }
            }

            viewModel.Item.CanJoin = true;

            if (room.IsPrivate && !isBroadcaster)
            {
                // 認証する
                if (!String.IsNullOrWhiteSpace(auth))
                {
                    string passCode = auth;
                    // 値が違う場合
                    if (passCode != room.AccessCode)
                    {
                        viewModel.Type    = MessageType.Error;
                        viewModel.Message = "認証コードが違います";

                        return(View(viewModel));
                    }
                }
                else
                {
                    viewModel.Type    = MessageType.Warning;
                    viewModel.Message = "このルームは認証が必要です";
                    return(View(viewModel));
                }
            }

            // アクセスを許可 -> Tokenを発行
            var tokenManager = TokenManager.GetInstance();
            var token        = tokenManager.CreateToken(room.Id);
            await db.SaveChangesAsync();

            TempData["token"] = token;

            return(RedirectToAction("LiveEmbedded", new { name = name }));
        }
示例#10
0
        public async Task <ActionResult> Join(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return(RedirectToAction("Index"));
            }

            var room = await db.Rooms.FirstOrDefaultAsync(c => c.Name == name);

            if (room == null)
            {
                return(HttpNotFound());
            }

            // ユーザーを取得できれば取得
            var viewModel = new StatusMessageViewModel <JoinRoomViewModel>()
            {
                Item = new JoinRoomViewModel()
                {
                    Room = room
                }
            };

            var appUser = await GetApplicationUser();

            bool isBroadcaster = (appUser != null && room.Owner.Id == appUser.Id);

            viewModel.Item.IsBroadcaster = isBroadcaster;

            if (!room.IsLive)
            {
                if (appUser == null || !appUser.OwnerRooms.Contains(room))
                {
                    // まだ入れませんよ
                    viewModel.Message = "現在ルームはオフラインです。管理者以外は入室できません。";
                    viewModel.Type    = MessageType.Error;
                    return(View(viewModel));
                }
                else if (isBroadcaster)
                {
                    var instance = RoomManager.GetInstance();
                    if (instance.GetRoomInfo(room.Id) == null)
                    {
                        // まだ入れませんよ
                        viewModel.Message = "あなたはこのルームの管理者です。ルームに入室するには、まずエディタからログインしてセッションを確立してください。";
                        viewModel.Type    = MessageType.Error;
                        return(View(viewModel));
                    }
                }
            }

            viewModel.Item.CanJoin = true;

            if (room.IsPrivate && !isBroadcaster)
            {
                // 認証する
                viewModel.Type         = MessageType.Warning;
                viewModel.Message      = "このルームは認証が必要です";
                viewModel.Item.CanJoin = true;
                return(View(viewModel));
            }

            return(View(viewModel));
        }
示例#11
0
        public async Task<ActionResult> JoinEmbedded(string name, string auth)
        {

            if (string.IsNullOrEmpty(name))
                return RedirectToAction("Index", "Rooms");

            // Join
            var room = await db.Rooms.FirstOrDefaultAsync(c => c.Name == name);
            if (room == null)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            // ユーザーを取得できれば取得
            var viewModel = new StatusMessageViewModel<JoinRoomViewModel>()
            {
                Item = new JoinRoomViewModel() { Room = room }
            };

            var appUser = await GetApplicationUser();
            bool isBroadcaster = (appUser != null && room.Owner.Id == appUser.Id);
            viewModel.Item.IsBroadcaster = isBroadcaster;

            if (!room.IsLive)
            {
                if (appUser == null || !appUser.OwnerRooms.Contains(room))
                {
                    viewModel.Message = "現在ルームはオフラインです。管理者以外は入室できません。";
                    viewModel.Type = MessageType.Error;
                    return View(viewModel);
                }
                else if (isBroadcaster)
                {
                    var instance = RoomManager.GetInstance();
                    if (instance.GetRoomInfo(room.Id) == null)
                    {
                        // まだ入れませんよ
                        viewModel.Message = "あなたはこのルームの管理者です。ルームに入室するには、まずエディタからログインしてセッションを確立してください。";
                        viewModel.Type = MessageType.Error;
                        return View(viewModel);
                    }
                }
            }

            viewModel.Item.CanJoin = true;

            if (room.IsPrivate && !isBroadcaster)
            {
                // 認証する
                if (!String.IsNullOrWhiteSpace(auth))
                {
                    string passCode = auth;
                    // 値が違う場合
                    if (passCode != room.AccessCode)
                    {
                        viewModel.Type = MessageType.Error;
                        viewModel.Message = "認証コードが違います";

                        return View(viewModel);
                    }
                }
                else
                {
                    viewModel.Type = MessageType.Warning;
                    viewModel.Message = "このルームは認証が必要です";
                    return View(viewModel);
                }
            }

            // アクセスを許可 -> Tokenを発行
            var tokenManager = TokenManager.GetInstance();
            var token = tokenManager.CreateToken(room.Id);
            await db.SaveChangesAsync();

            TempData["token"] = token;

            return RedirectToAction("LiveEmbedded", new { name = name });
        }
示例#12
0
        public async Task<ActionResult> Join(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return RedirectToAction("Index");
            }

            var room = await db.Rooms.FirstOrDefaultAsync(c => c.Name == name);
            if (room == null)
            {
                return HttpNotFound();
            }

            // ユーザーを取得できれば取得
            var viewModel = new StatusMessageViewModel<JoinRoomViewModel>()
            {
                Item = new JoinRoomViewModel() {Room = room}
            };

            var appUser = await GetApplicationUser();
            bool isBroadcaster = (appUser != null && room.Owner.Id == appUser.Id);
            viewModel.Item.IsBroadcaster = isBroadcaster;

            if (!room.IsLive)
            {
                if (appUser == null || !appUser.OwnerRooms.Contains(room))
                {
                    // まだ入れませんよ
                    viewModel.Message = "現在ルームはオフラインです。管理者以外は入室できません。";
                    viewModel.Type = MessageType.Error;
                    return View(viewModel);
                }
                else if (isBroadcaster)
                {
                    var instance = RoomManager.GetInstance();
                    if (instance.GetRoomInfo(room.Id) == null)
                    {
                        // まだ入れませんよ
                        viewModel.Message = "あなたはこのルームの管理者です。ルームに入室するには、まずエディタからログインしてセッションを確立してください。";
                        viewModel.Type = MessageType.Error;
                        return View(viewModel);
                    }
                }
            }

            viewModel.Item.CanJoin = true;

            if (room.IsPrivate && !isBroadcaster)
            {
                // 認証する
                viewModel.Type = MessageType.Warning;
                viewModel.Message = "このルームは認証が必要です";
                viewModel.Item.CanJoin = true;
                return View(viewModel);
            }

            return View(viewModel);
        }
        public async Task <IActionResult> ExternalLoginConfirmationAsync(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                StatusMessage = StatusMessageViewModel.Error("Error loading external login information during confirmation.");
                return(RedirectToAction("Login", new { ReturnUrl = returnUrl }));
            }

            IdentityResult result = IdentityResult.Success;

            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByLoginAsync(info.LoginProvider, info.ProviderKey);

                if (user == null)
                {
                    user = new User {
                        UserName = model.Email, Email = model.Email, DisplayName = model.Email,
                    };

                    if (info.Principal.HasClaim(t => t.Type == ClaimTypes.Name))
                    {
                        user.DisplayName = info.Principal.FindFirstValue(ClaimTypes.Name);
                    }
                    if (info.Principal.HasClaim(t => t.Type == ClaimTypes.GivenName))
                    {
                        user.DisplayName = info.Principal.FindFirstValue(ClaimTypes.GivenName);
                    }
                    if (info.Principal.HasClaim(t => t.Type == ClaimTypes.MobilePhone))
                    {
                        user.PhoneNumber = info.Principal.FindFirstValue(ClaimTypes.MobilePhone);
                    }

                    // TODO save claims

                    result = await _userManager.CreateAsync(user);

                    if (result.Succeeded)
                    {
                        result = await _userManager.AddLoginAsync(user, info);

                        if (result.Succeeded)
                        {
                            _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        }
                    }
                }

                if (result.Succeeded)
                {
                    if (!(await _userManager.IsEmailConfirmedAsync(user)) && _userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        await SendEmailConfirmationMessageAsync(user);

                        return(RedirectToAction("RegisterConfirmation", new { email = model.Email }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            return(View(model));
        }