在.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. Axure原型设计介绍

    在第八周的课堂上,王文娟老师在校园系统上发布了对于自行选择的原型设计软件进行资料查找以及自学的任务.因为之前的课程学习需要,我们大概掌握了原型设计软件Axure的使用,下面是一些我们学习过程中的介绍 ...

  2. 对接京东jos遇到的坑 记录一下。方便查询

    坑很多,有一些忘记了.文档乱的很,有问题可以私信我一下我看能不能想起来. 坑一.添加商品接口. {"error_response": {"code":" ...

  3. 关于javascript中时间格式和时间戳的转换

    当前时间获取的各种函数: var myDate = new Date();myDate.getYear();        //获取当前年份(2位),已经不推荐使用myDate.getFullYear ...

  4. C 标准库 - string.h之strcpy使用

    strcpy Copies the C string pointed by source into the array pointed by destination, including the te ...

  5. java实现任务调度

    最近的一个小项目是做一个简单的数据仓库,需要将其他数据库的数据抽取出来,并通过而出抽取成页面需要的数据,以空间换时间的方式,让后端报表查询更快. 因为在抽取的过程中,有一定的先后顺序,需要做一个任务调 ...

  6. shell -- 获取绝对路径

    readlink -f <file> readlink -m <file> 会把file的相对路径转化为绝对路径 几个选项的区别: -f, --canonicalize can ...

  7. Python基础(3) - 数据类型:3列表类型

    Python 列表是序列对象,可包含任意的Python数据信息,如字符串.数字.列表.元组等.列表的数据是可变的,我们可通过对象方法对列表中的数据进行增加.修改.删除等操作.列表用[]包括起来的. 列 ...

  8. step1: python & scrapy安装

    #首先安装python,这里安装python所需依赖包yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-dev ...

  9. 公司管理系列--80% of Your Culture is Your Founder(FaceBook)

    80% of Your Culture is Your Founder     When Molly Graham joined Facebook in 2008, the company still ...

  10. PostgreSQL On Windows Process Connection Performance

    本文主要对PostgreSql在Windows下的连接测试. 测试环境: Win7 x64, PostgreSql 10.1 x64 测试语言: VS2015 C# 因为Pg的数据库连接是开启进程来处 ...