作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取与当前用户关联的电子邮件。
下面显示了我在身份验证中添加 Claims 的几行。
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
try
{
CreateDataConnection();
R_AuthenticateUser oAuthUser = oDataConnection.Authenticate(context.UserName,context.Password);
string DB_User_roles = oAuthUser.UserLoginRoles;
if (oAuthUser.Authenticated)
{
string[] aray = DB_User_roles.Split(',');
identity.AddClaim(new Claim(ClaimTypes.Name, oAuthUser.UserID.ToString())); // keeps the login_ID
identity.AddClaim(new Claim(ClaimTypes.Email, context.UserName));
foreach (var item in aray)
{
// identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, item));
identity.AddClaim(new Claim(ClaimTypes.Role, item));
}
context.Validated(identity);
}
else //if (context.UserName == "user" && context.Password == "user")
{
context.SetError("Incorrect credntials", "Provided Username and Password is incorrect");
return;
}
}
catch (Exception ex)
{
int y = 0;
}
}
[HttpGet]
[PGAuthorization(Roles = "USER")]
[Route("api/Address/GetAllAddresses")]
public string GetAllAddressesByUser()
{
CreateDataConnection();
Int64 UserID = Convert.ToInt64((User as ClaimsPrincipal).Identity.Name);
List<R_CustomerAddress> oUser = oDataConnection.GetAllAddressesByUser(UserID);
string output = JsonConvert.SerializeObject(oUser);
return output;
}
Int64 UserID = Convert.ToInt64((User as ClaimsPrincipal).Identity.Email);
最佳答案
如果您在身份验证期间在声明中添加电子邮件,则可以通过以下方式获取:
string email = System.Security.Claims.ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value
关于asp.net - 如何从 ASP.net 中的主体获取电子邮件地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53845810/
我是一名优秀的程序员,十分优秀!