我在玩ASP。NET MVC5标识,并尝试实现基于声明的身份验证。
我得到以下错误:
无法隐式转换类型"System"。集合。通用的IEnumerable<lt;匿名类型:字符串主题,字符串类型,字符串值>>'to'系统。网状物Mvc。ActionResult"。存在显式转换(是否缺少强制转换?)
这是一段代码:
public ActionResult GetClaims()
{
var identity = User.Identity as ClaimsIdentity;
var claims = from c in identity.Claims
select new
{
subject = c.Subject.Name,
type = c.Type,
value = c.Value
};
return claims;
}
我以http://bitoftech.net/2015/03/31/asp-net-web-api-claims-authorization-with-asp-net-identity-2-1/
如果它在MVC控制器中,则应该返回一个视图,该视图接受IEnumerable<Claim>
作为模型:
public ActionResult GetClaims()
{
var identity = User.Identity as ClaimsIdentity;
var claims = from c in identity.Claims
select new
{
subject = c.Subject.Name,
type = c.Type,
value = c.Value
};
return View(claims);
}
如果它在api控制器中,则可以返回IHttpActionResult
public IHttpActionResult GetClaims()
{
var identity = User.Identity as ClaimsIdentity;
var claims = from c in identity.Claims
select new
{
subject = c.Subject.Name,
type = c.Type,
value = c.Value
};
return Ok(claims);
}
几件事。您正试图返回一个匿名类型的枚举值作为ActionResult
。通常,ActionResults希望您返回对传入模型的视图(剃刀模板)的引用:
return View(model);
如果你只想返回数据,那么你需要返回一个JsonResult
return Json(new { Data = model }, JsonRequestBehavior.AllowGet);