在.NET Core自带的Angular模板项目中,我想要做一个简单的登录认证。

所以想填写用户名密码,使用guid作为key,存储登录信息,每次页面刷新的时候check它。

思路觉得没有问题,但是一直失效,修改前代码:

 public class AuthController : Controller
{
private readonly IMemoryCache _cache;
public AuthController(IMemoryCache cache)
{
_cache = cache;
}
[HttpPost]
public IActionResult Post([FromBody]LoginModel model)
{
if (model != null && model.UserName == "xxxxx" && model.Password == "yyyyyyy")
{
var token = Guid.NewGuid();
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetPriority(CacheItemPriority.NeverRemove)
.SetSlidingExpiration(TimeSpan.FromDays());
_cache.Set(token, model, cacheEntryOptions);
return Ok(new { success = true, token = token, model= _cache.Get<LoginModel>(token) });
}
return Ok(new { success = false, error = "UserName or Password error." });
}
[HttpGet("check/{token}")]
public IActionResult Check(string token)
{
var model = _cache.Get<LoginModel>(token);
if (model != null && model.UserName == "xxxxx" && model.Password == "yyyyyyy")
{
return Ok(new { success = true, model });
}
return Ok(new { success = false, model });
} public class LoginModel
{
public string UserName { get; set; }
public string Password { get; set; }
}
}

但是在调用check的api时,就是找不到。

后来发现在set的地方,guid没有ToString,所以导致两次使用过的key不一样。

修改后:

   [Route("api/auth")]
public class AuthController : Controller
{
private readonly IMemoryCache _cache;
public AuthController(IMemoryCache cache)
{
_cache = cache;
}
[HttpPost]
public IActionResult Post([FromBody]LoginModel model)
{
if (!model.IsValidUserInfo())
return Ok(new { success = false, error = "UserName or Password error." });
var token = Guid.NewGuid().ToString();
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetPriority(CacheItemPriority.NeverRemove)
.SetSlidingExpiration(TimeSpan.FromDays());
_cache.Set(token, model, cacheEntryOptions);
return Ok(new { success = true, token = token });
}
[HttpGet("check/{token}")]
public IActionResult Check(string token)
{
var model = _cache.Get<LoginModel>(token);
return Ok(new { success = model.IsValidUserInfo() });
} public class LoginModel
{
public string UserName { get; set; }
public string Password { get; set; }
}
} public static class extController
{
public static bool IsValidUserInfo(this AuthController.LoginModel me)
=> me != null && me.UserName == "xxxx" && me.Password == "yyyy";
}

注:多加了一个扩展方法,验证登录信息。

记一次使用MemoryCache不能Get的问题的更多相关文章

  1. Netcore中简单使用MemoryCache

    用到缓存架构,我们一般都会想到的Redis,因为它支持分布式,高可用,速率非常快.MemoryCache则相对较少提到,但是对于单体项目或者小型项目,memorycache还是是不错的选择.Memor ...

  2. 记一次 .NET 某电商交易平台Web站 CPU爆高分析

    一:背景 1. 讲故事 已经连续写了几篇关于内存暴涨的真实案例,有点麻木了,这篇换个口味,分享一个 CPU爆高 的案例,前段时间有位朋友在 wx 上找到我,说他的一个老项目经常收到 CPU > ...

  3. Spark踩坑记——Spark Streaming+Kafka

    [TOC] 前言 在WeTest舆情项目中,需要对每天千万级的游戏评论信息进行词频统计,在生产者一端,我们将数据按照每天的拉取时间存入了Kafka当中,而在消费者一端,我们利用了spark strea ...

  4. Spark踩坑记——数据库(Hbase+Mysql)

    [TOC] 前言 在使用Spark Streaming的过程中对于计算产生结果的进行持久化时,我们往往需要操作数据库,去统计或者改变一些值.最近一个实时消费者处理任务,在使用spark streami ...

  5. 这些年一直记不住的 Java I/O

    参考资料 该文中的内容来源于 Oracle 的官方文档.Oracle 在 Java 方面的文档是非常完善的.对 Java 8 感兴趣的朋友,可以从这个总入口 Java SE 8 Documentati ...

  6. 千回百折:百度Java研发offer斩获记和经验分享

    起因 面试过程 等待offer的过程中悟道 Java面试常考知识点个人总结 过程 百度——作为国内互联网的巨头之一,最近的一些风波对其褒贬不一,但是类似事件不是第一次发生,也绝对不是最后一次,对于真的 ...

  7. 记一次nginx部署yii2项目时502 bad gateway错误的排查

    周六闲来无事,就试着安装和部署下yii2,安装过程没什么问题,但部署到nginx上时遇到了502 bad gatewary问题,折腾了半天才搞定.这个问题是我以前在部署yii2时没有遇到过的,因此记在 ...

  8. 【无私分享:ASP.NET CORE 项目实战(第十一章)】Asp.net Core 缓存 MemoryCache 和 Redis

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 经过 N 久反复的尝试,翻阅了网上无数的资料,GitHub上下载了十几个源码参考, Memory 和 Redis 终于写出一个 ...

  9. 原生JS实战:写了个一边玩游戏,一边记JS的API的游戏

    本文是苏福的原创文章,转载请注明出处:苏福CNblog:http://www.cnblogs.com/susufufu/p/5878913.html 本程序[一边玩游戏,一边记JS的API]是本人的个 ...

随机推荐

  1. hadoop踩坑:localhost:50070 无法访问 关闭防火墙

    ubuntu 关闭防火墙:ufw disable hadoop3.0以下版本web访问端口50070:3.0及以上web访问端口9870 参考链接:https://blog.csdn.net/qq_3 ...

  2. vue 2.0创建新项目

    参考链接  https://segmentfault.com/a/1190000011275993 背景在安装完node的基础上,机器什么都没安装参考上述链接 一.下载vue $ cnpm insta ...

  3. fastclick源码分析

    https://www.cnblogs.com/diver-blogs/p/5657323.html  地址 fastclick.js源码解读分析 阅读优秀的js插件和库源码,可以加深我们对web开发 ...

  4. 纯Python给ulaw wav文件加头

    最近在处理wav相关文件,碰见一工具产生的ualw文件不带header,顺手用python给wav格式文件加头处理,让普通播放器也能播放. (原文:http://www.cnblogs.com/ryh ...

  5. iOS 系统架构及常用框架(iOS的系统架构分为四个层次)

    1.iOS基于UNIX系统,因此从系统的稳定性上来说它要比其他操作系统的产品好很多 2.iOS的系统架构分为四层,由上到下一次为:可触摸层(Cocoa Touch layer).媒体层(Media l ...

  6. php多进程实现 亲测

    php多进程实现 PHP有一组进程控制函数(编译时需要–enable-pcntl与posix扩展),使得php能在nginx系统中实现跟c一样的创建子进程.使用exec函数执行程序.处理信号等功能. ...

  7. Reactjs事件处理的三种写法

    目录 前言 1. 在回调函数中使用箭头函数 2. 在构造器中绑定this 3. 使用类字段语法 事件参数的传递. 总结 前言 Reactjs中事件处理,与DOM元素处理类似,但也有一些不同的语法. R ...

  8. CSS实现响应式布局(自动拆分几列)

    1.css代码 <style type="text/css"> .container{ margin-top: 10px; } .outerDiv{ float:lef ...

  9. bnu Game 博弈。

    Game Time Limit: 10000ms Case Time Limit: 10000ms Memory Limit: 65536KB   64-bit integer IO format:  ...

  10. What is the relation of theme and it's derived theme.

    You know, a theme can derive from other theme in two ways: xx.xxx implicit way and parent="xxx& ...