Example #1
0
        public async Task <ActionResult <UserInfoVo> > GetInfo()
        {
            // get jwt
            StringValues input;

            Request.Headers.TryGetValue("Authorization", out input);
            string auth = input.ToString();
            string jwt  = auth.Substring("Bearer ".Length);

            if (string.IsNullOrEmpty(jwt))
            {
                return(new UnauthorizedResult());
            }

            // get username from orleans
            OrleanService orlean = await OrleanService.GetInstance();

            IValue grain    = orlean.GetValueGrain(jwt);
            string username = await grain.GetAsync();

            if (string.IsNullOrEmpty(username))
            {
                return(new UnauthorizedResult());
            }

            User user = await _context.Users.FindAsync(username);

            UserInfoVo ret = new UserInfoVo(user);

            return(new JsonResult(ret));
        }
Example #2
0
        public async Task <ActionResult <string> > PostLogin(LoginVo item)
        {
            // check params
            string ret = item.IsValid();

            if (!string.IsNullOrEmpty(ret))
            {
                return(ret);
            }

            // check has such user
            User user = await _context.Users.FindAsync(item.username.ToLower());

            if (user == null)
            {
                return("user not exists");
            }

            // check has logined
            OrleanService orlean = await OrleanService.GetInstance();

            IValue grain = orlean.GetValueGrain(item.username.ToLower());
            string jwt   = await grain.GetAsync();

            if (!string.IsNullOrEmpty(jwt))
            {
                return(jwt);
            }


            // check password
            if (user.Password != CryptoService.SHA1(item.password))
            {
                return("invalid password");
            }

            jwt = System.Guid.NewGuid().ToString();
            // cache session
            await grain.SetAsync(jwt);

            IValue grainR = orlean.GetValueGrain(jwt);
            await grainR.SetAsync(item.username.ToLower());

            return(jwt);
        }
Example #3
0
        public async Task OnActionExecutionAsync(ActionExecutingContext httpcontext, ActionExecutionDelegate next)
        {
            IPAddress ipAddress = httpcontext.HttpContext.Request.HttpContext.Connection.RemoteIpAddress;
            string    ip        = ipAddress.ToString();

            Console.WriteLine("\n\n\n\n--->from ip " + ipAddress);

            OrleanService orlean = await OrleanService.GetInstance();

            IRateLimit grain = orlean.GetRateLimit(ip);
            bool       isOk  = await grain.CheckRateLimit();

            if (!isOk)
            {
                BlackListItem item = await context.BlackLists.FindAsync(ip);

                if (item == null)
                {
                    item = new BlackListItem(ip);
                    context.BlackLists.Add(item);
                }
                else
                {
                    long unixTime = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
                    item.LastTime = unixTime;
                    context.BlackLists.Update(item);
                }
                await context.SaveChangesAsync();

                httpcontext.HttpContext.Response.StatusCode = 429;
                httpcontext.Result = new EmptyResult();
            }
            else
            {
                await next();
            }
        }