コード例 #1
0
ファイル: CommonController.cs プロジェクト: kuroiouji/c.s.i
 public async Task <IActionResult> IsAuthenticated([FromBody] Models.TokenDo t)
 {
     if (Utils.CommonUtil.IsNullOrEmpty(t.Value))
     {
         return(await this.ControllerResult(async (Web.Models.ResultData result) =>
         {
             if (User.Identity.IsAuthenticated)
             {
                 result.Data = await Task.FromResult <bool>(true);
             }
         }));
     }
     else
     {
         return(await RefreshToken(t));
     }
 }
コード例 #2
0
ファイル: CommonController.cs プロジェクト: kuroiouji/c.s.i
        public async Task <IActionResult> RefreshToken([FromBody] Models.TokenDo t)
        {
            return(await this.ControllerResult(async (Web.Models.ResultData result) =>
            {
                if (User.Identity.IsAuthenticated)
                {
                    Web.Models.User.ApplicationUser user = await _userManager.GetUserAsync(this.User);
                    if (user != null)
                    {
                        string path = Utils.Constants.TEMP_PATH;
                        path = System.IO.Path.Combine(path, "token_storage");
                        if (System.IO.Directory.Exists(path) == false)
                        {
                            System.IO.Directory.CreateDirectory(path);
                        }

                        path = System.IO.Path.Combine(path, t.Value);
                        if (System.IO.File.Exists(path))
                        {
                            using (System.IO.StreamReader rd = new System.IO.StreamReader(path, true))
                            {
                                string id = rd.ReadLine();
                                if (id == user.Id)
                                {
                                    var token = this.GenerateToken(user.UserName, user.Id);
                                    var refresh_token = Guid.NewGuid().ToString().Replace("-", "");

                                    result.Data = new
                                    {
                                        UserName = user.UserName,
                                        DisplayName = this._appDbContext.GetUserDisplayName(user.Id),
                                        GroupID = user.GroupID,
                                        Token = token,
                                        RefreshToken = refresh_token,
                                        Timeout = Convert.ToDouble(this._configuration["JwtExpireMinutes"])
                                    };

                                    string npath = Utils.Constants.TEMP_PATH;
                                    npath = System.IO.Path.Combine(npath, "token_storage");
                                    if (System.IO.Directory.Exists(npath) == false)
                                    {
                                        System.IO.Directory.CreateDirectory(npath);
                                    }
                                    npath = System.IO.Path.Combine(npath, refresh_token);
                                    if (System.IO.File.Exists(npath))
                                    {
                                        System.IO.File.Delete(npath);
                                    }

                                    using (System.IO.StreamWriter wr = new System.IO.StreamWriter(npath, true))
                                    {
                                        wr.WriteLine(user.Id);
                                    }
                                }
                            }

                            System.IO.File.Delete(path);
                        }
                    }
                }
            }));
        }