Asp.net核心MVC标识添加和获取



嘿,我已经用微软登录生成了模板。当你访问一个页面时,我想添加身份。我的代码:

int OsCislo = //Gets params from model
string name = //Gets params from model
List<System.Security.Claims.Claim> MyClaim = new List<System.Security.Claims.Claim>()
{
new System.Security.Claims.Claim (System.Security.Claims.ClaimTypes.Name, name),
new System.Security.Claims.Claim (System.Security.Claims.ClaimTypes.Role, OsCislo.ToString())
};
System.Security.Claims.ClaimsIdentity identity = new System.Security.Claims.ClaimsIdentity();
identity.AddClaims(MyClaim);
User.AddIdentity(identity);
///Find identity and read from it

我想做的事情:
1。获取数据
2。列出我放置数据的索赔清单
3。制作新的身份并提供声明
4。将标识添加到现有用户
5。通过用户查找该身份并从索赔中获取数据。

解决了它!如果有人想要这里的代码,它是:

int CisloG = Model.Where(s => s.UserName.Contains(name)).ToArray().First().One;
name = Model.Where(s => s.UserName.Contains(name)).ToArray().First().Two + " " + Model.Where(s => s.UserName.Contains(name)).ToArray().First().Three;
List<Claim> MyClaim = new List<System.Security.Claims.Claim>()
{
new Claim (ClaimTypes.GivenName, name),
new Claim (ClaimTypes.NameIdentifier, CisloG.ToString()),
new Claim (ClaimTypes.Email, User.Identity.Name)
};
ClaimsIdentity identity = new ClaimsIdentity();
identity.AddClaims(MyClaim);
User.AddIdentity(identity);
///Getting data
var Name = User.FindAll(ClaimTypes.GivenName).ToList().ElementAt(1).Value;
var OsCislo = User.FindAll(ClaimTypes.NameIdentifier).ToList().ElementAt(1).Value;
var Email = User.FindAll(ClaimTypes.Email).ToList().ElementAt(0).Value;
Console.WriteLine(Name + "(" + OsCislo + ") " + Email);

如果森博迪想知道它是做什么的,只需添加commment。

相关内容

  • 没有找到相关文章

最新更新