コード例 #1
0
        /// <summary>
        /// 初始化所有基础类的配置
        /// </summary>
        public static void Init()
        {
            try
            {
                //ResultInfo.ReadConfig();
                LoginHelp.ReadConfig();


                //ProductionInfo.ReadConfig();
                //Server.HttpServerInfo.ReadConfig();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                Environment.Exit(0);
            }
        }
コード例 #2
0
ファイル: Login.cs プロジェクト: Hanson0/OnlineWritingProcess
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string errReason;
            int    ret = LoginHelp.StartLogin(textUser.Text, textPassword.Text, out errReason);

            if (ret != 0)
            {
                labErrTip.BackColor = Color.FromArgb(251, 225, 227); //B:251, 225, 227  F:231, 61, 74
                labErrTip.ForeColor = Color.FromArgb(231, 61, 74);   //B:251, 225, 227  F:231, 61, 74
                labErrTip.Text      = errReason;
                textPassword.Text   = "";
                textUser.Focus();
                textUser.SelectAll();
                return;
            }
            loginOk = true;
            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请稍后");
            splashScreenManager1.SetWaitFormDescription("登录中...");
            Thread.Sleep(1500);
            splashScreenManager1.CloseWaitForm();
            this.Close();
        }
コード例 #3
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            if (username == _config["ADMIN_USERNAME"] && password == _config["ADMIN_PASSWORD"])
            {
                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, username));
                identity.AddClaim(new Claim(ClaimTypes.Name, "纳新系统管理员"));
                identity.AddClaim(new Claim(ClaimTypes.Sid, "0"));
                identity.AddClaim(new Claim(ClaimTypes.Role, "admin"));
                identity.AddClaim(new Claim(EvaClaimTypes.IsManager, "true"));
                var Iprincipal = new ClaimsPrincipal(identity);
                await HttpContext.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    Iprincipal,
                    new AuthenticationProperties
                {
                    IsPersistent = true,
                    AllowRefresh = true
                }
                    );

                return(RedirectToPage("/Index"));
            }
            var  NowSecret = EvaCryptoHelper.Password2Secret(password);
            long stuID     = 0;

            if (!long.TryParse(username, out stuID))
            {
                return(RedirectToPage("/Account/Denied"));
            }
            var query = from _user in _context.User
                        where _user.stuID == stuID
                        select _user;
            var user = await query.AsNoTracking().FirstOrDefaultAsync();

            //need use xms login service
            if (user == null)
            {
                Login login_Stu = new Login(username, password);

                //post login information to xms.zjueva.net and receive the response with string

                string  ansString = LoginHelp.PostMoths(LoginURL, login_Stu);
                JObject ansJson   = (JObject)JsonConvert.DeserializeObject(ansString);

                //error and Denied
                if (ansJson["status"].ToString() == "error")
                {
                    return(RedirectToPage("/Account/Denied"));
                }
                else if (ansJson["status"].ToString() == "success")
                {
                    User add_user = new User()
                    {
                        stuID     = long.Parse(ansJson["data"]["stuid"].ToString()),
                        Name      = ansJson["data"]["name"].ToString(),
                        Secret    = login_Stu.GetSHASecret(),
                        isManager = login_Stu.isManager()
                    };
                    _context.User.Add(add_user);
                    await _context.SaveChangesAsync();

                    //add new user to DB and now continue to create cookie;
                    user = add_user;
                    //this user is correct and  don't use the follow "else"
                }
            }
            if (user.Secret == NowSecret)
            {
                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
                identity.AddClaim(new Claim(ClaimTypes.Name, user.Name));
                identity.AddClaim(new Claim(ClaimTypes.Sid, user.Uid));
                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, username));
                if (user.isManager)
                {
                    identity.AddClaim(new Claim(EvaClaimTypes.IsManager, "true"));
                    identity.AddClaim(new Claim(ClaimTypes.Role, "manager"));
                }
                else
                {
                    identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
                }
                var Iprinciple = new ClaimsPrincipal(identity);
                await HttpContext.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    Iprinciple,
                    new AuthenticationProperties
                {
                    IsPersistent = true,
                    AllowRefresh = true
                }
                    );

                return(RedirectToPage("/Records/Index"));
            }

            //if user change his secret in XMS
            else
            {
                Login login_Stu = new Login(username, password);

                //post login information to xms.zjueva.net and receive the response with string

                string  ansString = LoginHelp.PostMoths(LoginURL, login_Stu);
                JObject ansJson   = (JObject)JsonConvert.DeserializeObject(ansString);

                //error and Denied
                if (ansJson["status"].ToString() == "error")
                {
                    return(RedirectToPage("/Account/Denied"));
                }
                else
                {
                    //change the secret in  DB
                    user = await _context.User.FirstOrDefaultAsync(r => r.stuID == stuID);

                    user.Secret = login_Stu.GetSHASecret();
                    _context.Attach(user).State = EntityState.Modified;
                    try
                    {
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        return(NotFound());
                    }
                    return(RedirectToPage("/Index"));
                }
            }
        }