asp.net – 在成功登录时添加声明并在应用程序的其他位置检索它
发布时间:2020-05-28 02:55:27 所属栏目:asp.Net 来源:互联网
导读:请求我帮助实现向经过身份验证的用户分配声明的自定义方式. 成功登录后, var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); switch (result) {
|
请求我帮助实现向经过身份验证的用户分配声明的自定义方式.
var result = await SignInManager.PasswordSignInAsync(model.Email,model.Password,model.RememberMe,shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
//Get the user
ApplicationUser user = UserManager.FindByEmail(model.Email);
//Ends here
ClaimsIdentity identity = await UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true },identity);
我使用userId从数据存储区中获取有关用户的角色和其他信息.此后,我需要在重定向到用户仪表板之前,使用电子邮件,角色,名字,姓氏,性别等信息添加有关用户的声明. 例如,当我想在登录部分显示电子邮件索赔值时,就像这样 var claims = ClaimsPrincipal.Current.Claims;
var principal = (ClaimsPrincipal)Thread.CurrentPrincipal;
var email = principal.Claims.Where(c => c.Type == ClaimTypes.Email).Select(c => c.Value).SingleOrDefault();
它返回null. 因此,我只能在添加它们后使用相同的登录方法访问它们,但我需要能够从应用程序的任何位置访问它. 谢谢. 解决方法您必须在登录前添加您的声明,而不是之后.考虑这个例子:public async Task<ActionResult> Login(LoginViewModel model,string returnUrl)
{
var user = UserManager.Find(model.Email,model.Password);
if(user!=null)
{
var ident = UserManager.CreateIdentity(user,DefaultAuthenticationTypes.ApplicationCookie);
ident.AddClaims(new[] {
new Claim("MyClaimName","MyClaimValue"),new Claim("YetAnotherClaim","YetAnotherValue"),});
AuthenticationManager.SignIn(
new AuthenticationProperties() { IsPersistent = true },ident);
return RedirectToLocal(returnUrl);
}
ModelState.AddModelError("","Invalid login attempt.");
return View(model);
}
现在,由于我们在登录时注入了索赔,因此我们可以随时随地访问索赔: ((ClaimsIdentity)User.Identity).FindFirst("MyClaimName");
您还可以在ApplicationUser.GenerateUserIdentityAsync()方法中添加声明.通过在此方法中添加声明,您可以使用SignInManager.PasswordSignInAsync()方法登录用户,而无需修改默认的Login操作方法. public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
userIdentity .AddClaims(new[] {
new Claim("MyClaimName",});
return userIdentity;
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 在同一个域上的两个网站之间共享cookie
- asp.net-core – Netcore 2.1.1版本导致应用程序无法运行
- iis – 如何查看池中的哪个asp.net应用程序使用的内存最多
- asp.net – 如何在Firefox上命名文件以供下载?
- asp.net-mvc – 如何包括一个模型与RedirectToAction?
- ASP.NET C#SignalR流到客户端
- 清除ASP.NET临时文件缓存的麻烦
- asp.net-mvc – 如何应用css类到mvccontrib网格
- asp.net-core – .Net 5中的调试设置
- asp.net – 如何扩展aspnet成员身份验证表?
推荐文章
站长推荐
- asp.net – 成员资格生成密码仅字母数字密码?
- asp.net-mvc-3 – 使用EditorFor/TextBoxFor/Tex
- asp-classic – 检索ADO Recordset字段名称(经典
- asp.net – 将通用模型的子类传递给剃刀视图
- asp.net – Azure网站上脚本/样式的长时间等待(T
- 下载ASP.NET MVC C#中字节数组列表中包含的多个文
- asp.net – 如何全局创建CustomPrincipal(使用和
- 如何在页面加载后从代码设置ASP.NET标签文本?
- asp.net-mvc – ASP.NET MVC / IIS 7.5:500内部
- asp.net-mvc – 将类应用于@ Html.ValidationMes
热点阅读
