Exemple #1
0
        private void MobileLogin(OAuthGrantResourceOwnerCredentialsContext context)
        {
            AuthCustomer auth = new AuthCustomer();

            using (PeContext db = new PeContext())
            {
                var smsLogs   = db.SMSLog.Where(x => x.phone == context.UserName && x.status == ConstValue.SmsStatus.Pending && x.smsType == ConstValue.SmsType.Login).OrderByDescending(x => x.createTime).ToList();
                var available = smsLogs.Where(x => x.createTime >= DateTime.Now.AddMinutes(-ConstValue.SmsEffectiveTime)).FirstOrDefault();
                if (available != null)
                {
                    if (context.Password == available.code)
                    {
                        var customer = db.Customer.Where(x => x.MOBILE == context.UserName).FirstOrDefault();
                        if (customer == null)
                        {
                            Customer cus = new Customer()
                            {
                                MOBILE      = context.UserName,
                                CREATE_TIME = DateTime.Now,
                            };
                            db.Customer.Add(cus);
                            db.SaveChanges();

                            customer = db.Customer.Where(x => x.MOBILE == context.UserName).FirstOrDefault();
                        }

                        customer.LAST_LOGIN_TIME = DateTime.Now;
                        db.SaveChanges();

                        ClearSmsLogs(smsLogs, available, db);

                        auth = new AuthCustomer()
                        {
                            CustomerId = customer.CUSTOMER_ID,
                            Mobile     = customer.MOBILE,
                            RealName   = customer.REAL_NAME,
                        };

                        string json = JsonConvert.SerializeObject(auth);

                        var identity = new ClaimsIdentity(context.Options.AuthenticationType);

                        identity.AddClaim(new Claim("sub", json));
                        identity.AddClaim(new Claim("role", "user"));

                        context.Validated(identity);
                        return;
                    }
                }
            }

            context.SetError("invalid_grant", "验证码不正确。");
        }
Exemple #2
0
        private void NormalLogin(OAuthGrantResourceOwnerCredentialsContext context)
        {
            AuthCustomer auth = new AuthCustomer();

            using (PeContext db = new PeContext())
            {
                var customer = db.Customer.Where(x => x.MOBILE == context.UserName.Trim()).FirstOrDefault();
                if (customer == null)
                {
                    context.SetError("invalid_grant", "用户名或密码不正确。");
                    return;
                }

                if (customer.USER_PASSWORD.Trim() != context.Password.Trim())
                {
                    context.SetError("invalid_grant", "用户名或密码不正确。");
                    return;
                }

                customer.LAST_LOGIN_TIME = DateTime.Now;
                db.SaveChanges();

                auth = new AuthCustomer()
                {
                    CustomerId = customer.CUSTOMER_ID,
                    Mobile     = customer.MOBILE,
                    RealName   = customer.REAL_NAME,
                };
            }

            string json = JsonConvert.SerializeObject(auth);

            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim("sub", json));
            identity.AddClaim(new Claim("role", "user"));

            context.Validated(identity);
        }