Пример #1
0
        public ActionResult Login(String username, String password)
        {
            if (ModelState.IsValid)
            {
                USER user = AuthenticationModel.AuthenticateUser(username, password);

                if (user == null)
                {
                    ViewBag.ErrorMessage = ErrorMessage.INVALID_LOGIN;
                    return(View());
                }

                LoginSession.LoginSession(user);

                if (LoginSession.ISAdmin())
                {
                    return(RedirectToAction("DashBoard", "Home"));
                }
                else if (LoginSession.ISStaff())
                {
                    return(RedirectToAction("Index", "Book"));
                }
                else if (LoginSession.ISCustomer())
                {
                    return(RedirectToAction("BookList", "Book"));
                }
            }
            return(View());
        }
Пример #2
0
        public ActionResult Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var authRet = AuthenticationModel.AuthenticateUser(model);

                    if (String.IsNullOrWhiteSpace(authRet))
                    {
                        return(Redirect("~/Home"));
                    }

                    ModelState.AddModelError(string.Empty, authRet);
                    return(View("Login", model));
                }
                catch (Exception ex)
                {
                    //LoggingHelper.Logger.LogException(ex, typeof(AuthenticationController), "In Login method");
                    LoggingHelper.Logger.WriteException(ex);
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
            }
            return(View("Login", model));
        }
        public UserAuthorizationController(ITokenizer tokenizer)
        {
            Post["/login/"] = x =>
            {
                using (var dbWrapper = new DbWrapper("AuthenticationDbCore"))
                {
                    if (!dbWrapper.DoesDbExist())
                    {
                        StudyBuddyDbAssistant.CreateDatabase("AuthenticationDbCore");
                        StudyBuddyDbAssistant.CreateAuthenticationTables("AuthenticationDbCore");
                    }
                }
                var loginData = ParseAuthData(Request.Body);
                var identity  = AuthenticationSingleton.AuthenticateUser(loginData["username"],
                                                                         loginData["password"]);
                if (identity == null)
                {
                    var response = (Response)JsonConvert.SerializeObject(FormErrorResponse(-1));
                    response.ContentType = "application/json";
                    response.StatusCode  = HttpStatusCode.NotAcceptable;
                    return(response);
                }
                else
                {
                    var token = tokenizer.Tokenize(identity, Context);
                    return(new
                    {
                        Token = token
                    });
                }
            };

            Post["/register/"] = x =>
            {
                var regData = ParseAuthData(Request.Body);
                var authenticationStatus = AuthenticationSingleton.RegisterUser(regData["username"], regData["password"]);
                if (authenticationStatus != 0)
                {
                    var response = (Response)JsonConvert.SerializeObject(FormErrorResponse(authenticationStatus));
                    response.ContentType = "application/json";
                    response.StatusCode  = HttpStatusCode.NotAcceptable;
                    return(response);
                }
                using (var dbWrapper = new DbWrapper("AuthenticationDbCore"))
                {
                    if (!dbWrapper.DoesDbExist())
                    {
                        StudyBuddyDbAssistant.CreateDatabase("AuthenticationDbCore");
                        StudyBuddyDbAssistant.CreateAuthenticationTables("AuthenticationDbCore");
                    }
                }

                var identity = AuthenticationSingleton.AuthenticateUser(regData["username"],
                                                                        regData["password"]);
                if (identity == null)
                {
                    var response = (Response)JsonConvert.SerializeObject(FormErrorResponse(-1));
                    response.ContentType = "application/json";
                    response.StatusCode  = HttpStatusCode.NotAcceptable;
                    return(response);
                }
                else
                {
                    var token = tokenizer.Tokenize(identity, Context);
                    return(new
                    {
                        Token = token
                    });
                }
            };
        }