如何在ASP.NET Core中使用Cookie中间件的详细介绍


当前第2页 返回上一页

1

2

3

4

5

6

7

await HttpContext.Authentication.SignInAsync(

  "MyCookieMiddlewareInstance",

  principal,

  new AuthenticationProperties

  {

   ExpiresUtc = DateTime.UtcNow.AddMinutes(20)

  });

这段代码将创建一个身份认证和相应的cookie且将持续20分钟。 任何在Cookie options中配置的动态选项都会被忽略。 ExpiresUtc 和 IsPersistent 这两个属性是相互独立的。

其实上面bb了那么多,都没用! 不如来个demo

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

// 1. 在Startup.cs的Configure方法中加上

app.UseCookieAuthentication(new CookieAuthenticationOptions

{

 AuthenticationScheme = "UserAuth",  // Cookie 验证方案名称,在写cookie时会用到。

 AutomaticAuthenticate = true,     // 是否自动启用验证,如果不启用,则即便客服端传输了Cookie信息,服务端也不会主动解析。除了明确配置了 [Authorize(ActiveAuthenticationSchemes = "上面的方案名")] 属性的地方,才会解析,此功能一般用在需要在同一应用中启用多种验证方案的时候。比如分Area.

 LoginPath = "/User/Index"     // 登录页

});

 

// 2. 新建UserController

// 3. 创建一个测试登录的方法(这里为了方便测是我用的是get方法,方便传参请求)

public IActionResult Login(int userId, string userName)

{

 WriteUser(userId, userName);

 return Content("Write");

}

 

private async void WriteUser(int userId, string userName)

{

 var identity = new ClaimsIdentity("Forms");  // 指定身份认证类型

 identity.AddClaim(new Claim(ClaimTypes.Sid, userId.ToString()));  // 用户Id

 identity.AddClaim(new Claim(ClaimTypes.Name, userName));       // 用户名称

 var principal = new ClaimsPrincipal(identity);

 await HttpContext.Authentication.SignInAsync("UserAuth", principal, new AuthenticationProperties { IsPersistent = true , ExpiresUtc = DateTime.UtcNow.AddMinutes(20) }); //过期时间20分钟

}

 

// 4. 创建一个退出登录的方法

public async Task<ActionResult> Logout()

{

 await HttpContext.Authentication.SignOutAsync("UserAuth"); // Startup.cs中配置的验证方案名

 return RedirectToAction("User", "Index");

}

 

// 5. 创建一个获取cookie用户信息的方法方便调用

private int GetUserId()

{

 //var userName = User.Identity.Name; //获取登录时存储的用户名称

 var userId = User.FindFirst(ClaimTypes.Sid).Value; // 获取登录时存储的Id

 if (string.IsNullOrEmpty(userId))

 {

  return 0;

 }

 else

 {

  return int.Parse(userId);

 }

}

// 或者写一个测试Action

public JsonResult CheckLogin()

{

 var userName = User.Identity.Name; //获取登录时存储的用户名称

 var userId = User.FindFirst(ClaimTypes.Sid).Value; // 获取登录时存储的Id

 return Json({UserId:userId,UserName:userName});

}

 

// 6. 以上是加密的方式如果直接写好像也是可以的

HttpContext.Response.Cookies.Append("Key", "Value");

以上就是如何在ASP.NET Core中使用Cookie中间件的详细介绍的详细内容!

返回前面的内容

相关阅读 >>

asp.net中partial class部分类

asp.net mvc 中获取当前url、controller、action图文实例

asp.net反射简单应用实例代码

asp.net完成文件上传的代码教程

.net core 和 .net .framework 相比哪个速度快?

asp.net实现简单数字验证码实例

如何在不使用 webmatrix 的情况下发布 web pages?

浅析asp.net使用image/imagebutton空间显示本地图片的方法

关于asp.net如何利用ajaxpro完成前端跟后台交互的实例分析

详细分析.net?core?以及与?.net?framework的关系(图)

更多相关阅读请进入《asp.net》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...