C# 在Web Api 2中获取HttpContext.Current.User.Identity.Name null通过Web Api 4登录 bool iss=HttpContext.Current.User.Identity.IsAuthenticated; 国际空间站{ UserManager.FindByName(HttpContext.Current.User.Identity.Name); 字符串名称=HttpContext.Current.User.Identity.name } 我得到的名称为空,并通过身份验证是真实登录 带有angular 2的asp.net web api 上面给出了Web配置,我也尝试mode=“Windows”
我解决了我的问题,我使用了C# 在Web Api 2中获取HttpContext.Current.User.Identity.Name null通过Web Api 4登录 bool iss=HttpContext.Current.User.Identity.IsAuthenticated; 国际空间站{ UserManager.FindByName(HttpContext.Current.User.Identity.Name); 字符串名称=HttpContext.Current.User.Identity.name } 我得到的名称为空,并通过身份验证是真实登录 带有angular 2的asp.net web api 上面给出了Web配置,我也尝试mode=“Windows”,c#,angular,asp.net-web-api,asp.net-identity,C#,Angular,Asp.net Web Api,Asp.net Identity,我解决了我的问题,我使用了GenerateUserIdentityAsync()方法而不是此方法 bool iss = HttpContext.Current.User.Identity.IsAuthenticated; if(iss) { UserManager.FindByName(HttpContext.Current.User.Identity.Name); string name = HttpContext.Current.User.Iden
GenerateUserIdentityAsync()
方法而不是此方法
bool iss = HttpContext.Current.User.Identity.IsAuthenticated;
if(iss) {
UserManager.FindByName(HttpContext.Current.User.Identity.Name);
string name = HttpContext.Current.User.Identity.Name
}
<authentication mode="None" />
<remove name="FormsAuthentication" />
我的代码现在运行良好
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
public override异步任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext)
{
var userStore=new CustomUserStore(new ApplicationDbContext());
var-manager=新的UserManager(userStore);
var user=wait manager.FindAsync(context.UserName,context.Password);
如果(用户!=null)
{
//var identity=newclaimsidentity(context.Options.AuthenticationType);
ClaimsIdentity identity=wait user.GenerateUserIdentityAsync(manager);
AddClaim(新声明(“UserId”,user.Id.ToString());
identity.AddClaim(新声明(“Username”,user.Username.ToString());
identity.AddClaim(新声明(“电子邮件”,user.Email));
AddClaim(新声明(“UserRole”,新RoleManagerBusinessLogic().GetRoleNameByRoleId(user.Roles.FirstOrDefault().RoleId));
AddClaim(新声明(“LoggedOn”,DateTime.UtcNow.ToString());
上下文验证(身份);
}
其他的
返回;
}
我解决了我的问题,我使用了GenerateUserIdentityAsync()
方法而不是此方法
bool iss = HttpContext.Current.User.Identity.IsAuthenticated;
if(iss) {
UserManager.FindByName(HttpContext.Current.User.Identity.Name);
string name = HttpContext.Current.User.Identity.Name
}
<authentication mode="None" />
<remove name="FormsAuthentication" />
我的代码现在运行良好
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
public override异步任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext)
{
var userStore=new CustomUserStore(new ApplicationDbContext());
var-manager=新的UserManager(userStore);
var user=wait manager.FindAsync(context.UserName,context.Password);
如果(用户!=null)
{
//var identity=newclaimsidentity(context.Options.AuthenticationType);
ClaimsIdentity identity=wait user.GenerateUserIdentityAsync(manager);
AddClaim(新声明(“UserId”,user.Id.ToString());
identity.AddClaim(新声明(“Username”,user.Username.ToString());
identity.AddClaim(新声明(“电子邮件”,user.Email));
AddClaim(新声明(“UserRole”,新RoleManagerBusinessLogic().GetRoleNameByRoleId(user.Roles.FirstOrDefault().RoleId));
AddClaim(新声明(“LoggedOn”,DateTime.UtcNow.ToString());
上下文验证(身份);
}
其他的
返回;
}