【.net framework基础之】claim

9 篇文章 0 订阅
6 篇文章 0 订阅

添加claim

var claims = new List<Claim>();
//claims.Add(new Claim(ClaimTypes.Name, model.UserName));
claims.Add(new Claim("userName", model.UserName));
claims.Add(new Claim("member", model.Member));

 

获取claim

IEnumerable<Claim> claims = HttpContext.GetOwinContext().Authentication.User.Claims;
string member = claims.ToList().Find(x => x.Type == "member").Value.ToString();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Json.NET http://james.newtonking.com/projects/json-net.aspx http://www.codeplex.com/json/ Description: The Json.NET library makes working with JSON formatted data in .NET simple. Quickly read and write JSON using the JsonReader and JsonWriter or serialize your .NET objects with a single method call using the JsonSerializer. Json.NET's features include: - Lightning fast JsonReader and JsonWriter - The JsonSerializer for quickly converting your .NET objects to JSON and back again - Json.NET can optionally produce well formatted, indented JSON for debugging or display - Attributes like JsonIgnore and JsonProperty can be added to a class to customize how a class is serialized - Ability to convert JSON to and from XML Instructions: 1. Extract Newtonsoft.Json.dll and Newtonsoft.Json.xml from the archive's /bin directory into your own applications. 2. Add a reference to Newtonsoft.Json.dll within Visual Studio.NET to your project. Silverlight: Json.NET has a build that works with the Silverlight .NET runtime. Reference the Newtonsoft.Json.Silverlight.dll from your Silverlight project. License: Copyright (c) 2007 James Newton-King Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ASP.NET身份验证是一种用于验证用户身份的机制,它可以确保只有经过身份验证的用户才能访问应用程序的受保护部分。ASP.NET Core提供了多种身份验证方式,包括Cookie身份验证、JWT身份验证、OpenID Connect身份验证等。其中,Cookie身份验证是最常用的一种方式。 以下是ASP.NET Core中使用Cookie身份验证的示例代码: 1. 在Startup.cs文件的ConfigureServices方法中添加身份验证服务: ```csharp services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.LoginPath = "/Account/Login"; options.LogoutPath = "/Account/Logout"; }); ``` 2. 在Startup.cs文件的Configure方法中启用身份验证中间件: ```csharp app.UseAuthentication(); ``` 3. 在需要进行身份验证的Controller或Action上添加[Authorize]特性: ```csharp [Authorize] public class HomeController : Controller { // ... } ``` 4. 在登录Controller中使用SignInAsync方法进行登录: ```csharp public async Task<IActionResult> Login(LoginViewModel model) { // 验证用户名和密码 if (/* 验证通过 */) { // 创建用户标识 var claims = new List<Claim> { new Claim(ClaimTypes.Name, model.UserName) }; var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); // 登录 await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity)); // 跳转到首页 return RedirectToAction("Index", "Home"); } // 验证失败,返回登录页面 return View(model); } ``` 5. 在注销Controller中使用SignOutAsync方法进行注销: ```csharp public async Task<IActionResult> Logout() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return RedirectToAction("Index", "Home"); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值