public ActionResult CreateToken(string username, string password) { DataResult result = new DataResult(); //假设用户名为"admin",密码为"123" if (username == "admin" && password == "123") { var payload = new LoginInfo() { Name = username, PassWord = password }; result.Token = JwtHelper.EncodeJwt(payload); result.Success = true; result.Message = "成功"; } else { result.Token = ""; result.Success = false; result.Message = "生成token失败"; } return(Json(result)); }
public async Task <string> LoginAndGenerateJwtToken(string account, string password) { var user = await VerifyPassword(account, password); if (user == null) { return(null); } var isAdmin = await _context.UserGroup.Where(x => x.IsAdmin).Select(x => x.Id) .Intersect(_context.UserGroupMember.Where(x => x.UserId == user.Id) .Select(x => x.UserGroupId)).AnyAsync(); return(_jwtHelper.EncodeJwt(new DefaultJwtTokenModel() { LoginId = Guid.NewGuid().ToString(), Expiration = DateTimeOffset.UtcNow.AddDays(1).ToUnixTimeSeconds(), TokenId = Guid.NewGuid().ToString(), TokenType = "LOGIN", UserAccount = account, LoginProviderId = null, UserId = user.Id.ToString(), Role = isAdmin ? "Admin" : "User" })); }
public JsonResult CreateToken(string username, int timeOut) { DataResult result = new DataResult(); var jwtModel = new JWTModel(); jwtModel.UserName = username; jwtModel.Expiration = DateTime.Now.AddMilliseconds(timeOut);; result.Token = JwtHelper.EncodeJwt(jwtModel); result.Success = true; result.Message = "成功"; return(Json(result)); //get请求需要修改成这样 //return Json(result, JsonRequestBehavior.AllowGet); }
public string GenerateJwt(DefaultJwtTokenModel tokenModel) { return("bearer " + _jwtHelper.EncodeJwt(tokenModel)); }