Exemplo n.º 1
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInHelper.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Exemplo n.º 2
0
        //[ValidateAntiForgeryToken]
        public async Task <IHttpActionResult> Register(CandidateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    model.AuthID = user.Id;
                    try
                    {
                        await _candidateService.CreateCandidateAsync(model);
                    }
                    catch (Exception ex)
                    {
                    }
                    await SignInHelper.SignInAsync(user, false, false);

                    return(Ok(true));
                }
            }

            // If we got this far, something failed, redisplay form
            return(Ok(true));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInHelper.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // 有关如何启用帐户确认和密码重置的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=320771
                    // 发送包含此链接的电子邮件
                    // 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, "确认你的帐户", "请通过单击 <a href=\"" + callbackUrl + "\">這裏</a>来确认你的帐户");

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return(View(model));
        }
Exemplo n.º 4
0
        public async Task <HttpResponseMessage> SignIn(UserSignIn model)
        {
            if (model == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No Data"));
            }
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards lockout only two factor authentication
                // To enable password failures to trigger lockout, change to shouldLockout: true
                var result = await SignInHelper.PasswordSignIn(model.UserName, model.Password, true, false);

                switch (result)
                {
                case Ullo.Models.SignInStatus.Success:
                    var user = await users.FindByNameAsync(model.UserName);

                    await SignInHelper.SignInAsync(user, true, false);

                    var account = new UserDetail();
                    account.UserName        = user.UserName;
                    account.FirstName       = user.FirstName;
                    account.LastName        = user.LastName;
                    account.FacebookId      = user.FacebookId;
                    account.isAuthenticated = true;
                    account.Route           = user.Route;
                    var rolesForUser = users.GetRoles(user.Id);
                    account.isAdmin = rolesForUser.Contains("Admin");
                    return(Request.CreateResponse(account));

                case Ullo.Models.SignInStatus.LockedOut:
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "LockedOut"));

                case Ullo.Models.SignInStatus.RequiresTwoFactorAuthentication:
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "RequiresTwoFactorAuthentication"));

                case Ullo.Models.SignInStatus.Failure:
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Failure"));

                default:
                    return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Invalid"));
                }
            }
            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
        }
Exemplo n.º 5
0
        public async Task <HttpResponseMessage> Signup(UserSignUp model)
        {
            if (model == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No Data"));
            }
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName        = model.UserName,
                    Email           = model.Email,
                    FirstName       = model.FirstName,
                    LastName        = model.LastName,
                    FacebookId      = model.FacebookId,
                    FacebookPicture = model.FacebookPicture,
                    FacebookToken   = model.FacebookToken,
                    Route           = IdentityModels.getNameAsRoute(model.UserName),
                };
                var result = await users.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInHelper.SignInAsync(user, true, false);

                    var account = new UserDetail();
                    account.UserName        = user.UserName;
                    account.FirstName       = user.FirstName;
                    account.LastName        = user.LastName;
                    account.FacebookId      = user.FacebookId;
                    account.isAuthenticated = true;
                    account.Route           = user.Route;
                    var rolesForUser = users.GetRoles(user.Id);
                    account.isAdmin = rolesForUser.Contains("Admin");
                    return(Request.CreateResponse(account));
                    // var code = await users.GenerateEmailConfirmationTokenAsync(user.Id);
                    // send email confirmation token
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, result.Errors.FirstOrDefault()));
                }
            }
            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
        }
Exemplo n.º 6
0
        public async Task <HttpResponseMessage> SignInWithFacebook(FacebookSignIn model)
        {
            if (model == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No Data"));
            }
            if (ModelState.IsValid)
            {
                var client             = new FacebookClient(model.AccessToken);
                Facebook.JsonObject me = (Facebook.JsonObject)client.Get("/me?fields=name,first_name,last_name,id,email");

                ApplicationUser user = db.Users.Where(x => x.FacebookId == model.UserID).FirstOrDefault();
                if (user != null)
                {
                    user.FacebookToken = model.AccessToken;
                    await db.SaveChangesAsync();

                    // if (user.FacebookToken == model.AccessToken) {
                    await SignInHelper.SignInAsync(user, true, true);

                    var account = new UserDetail();
                    account.UserName        = user.UserName;
                    account.FirstName       = me["first_name"].ToString(); // user.FirstName;
                    account.LastName        = user.LastName;
                    account.FacebookId      = user.FacebookId;
                    account.isAuthenticated = true;
                    account.Route           = user.Route;
                    var rolesForUser = users.GetRoles(user.Id);
                    account.isAdmin = rolesForUser.Contains("Admin");
                    return(Request.CreateResponse(account));

                    /*
                     * } else {
                     *  return Request.CreateErrorResponse(HttpStatusCode.MethodNotAllowed, "Facebook token differs");
                     * }
                     * */
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Facebook id not found"));
                }
            }
            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
        }
Exemplo n.º 7
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInHelper.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            return(View(model));
        }
Exemplo n.º 8
0
        public async Task <ActionResult> OnPostAsync([FromServices] AuthenticationReader authenticationReader)
        {
            if (ModelState.IsValid)
            {
                InfoForLoginPostHandler userInfo = await authenticationReader.GetInfoForLoginPostHandler(Form.Email);

                if (userInfo is null || PasswordHashHelper.CheckPasswordHash(Form.Password, userInfo.HashedPassword) == false)
                {
                    ModelState.AddModelError("", "The email or password is incorrect.");
                }
                else
                {
                    var authenticationTicketInfo = new SignInHelper.AuthenticationTicketInfo
                    {
                        UserId    = userInfo.UserId,
                        FirstName = userInfo.FirstName,
                        AuthTicketInfoLastChangeUtcTime = userInfo.AuthTicketInfoLastChangeUtcTime
                    };
                    await SignInHelper.SignInAsync(authenticationTicketInfo, Form.RememberMe, HttpContext);
                }
            }
Exemplo n.º 9
0
        public async Task <ActionResult> WeixinLogin(string code, string state)
        {
            //if (string.IsNullOrEmpty(code))
            //{
            //    return RedirectToAction("Login");
            //}

            if (string.IsNullOrEmpty(state))
            {
                state = "~/Manage/index";
            }
            var weixinauthstatus = UserBackendService.IsWeiXinAuth(code);

            if (weixinauthstatus.AuthStatus)
            {
                var user = ReadOnlyRepository.Get(e => e.UserBackendId == weixinauthstatus.UserId.Value);
                if (user == null)
                {
                    ViewBag.State = state;
                    return(View());
                }
                #region 重新登录(跳过账号和密码)
                var userbankend = await UserManager.FindByNameAsync(user.UserName);

                if (userbankend == null)
                {
                    ViewBag.State = state;
                    return(View());
                }
                await SignInHelper.SignInAsync(userbankend, false, false);

                log4net.LogManager.GetLogger("RollingLog").Info(user.UserName + " Log in: 微信登录");
                #endregion
                //地址要改
                return(RedirectToLocal(state));
            }
            ViewBag.IsWeixin = !string.IsNullOrEmpty(code);
            ViewBag.State    = state;
            return(View());
        }