无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Web.Mvc.ActionResult'

6de*_*il6 1 asp.net-mvc claims-based-identity asp.net-identity

我正在使用ASP.NET MVC5 Identity并尝试实现基于声明的身份验证.

我收到以下错误:

无法隐式转换类型'System.Collections.Generic.IEnumerable << anonymous type:string subject,string type,string value >>'to System.Web.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;
}
Run Code Online (Sandbox Code Playgroud)

我正在关注http://bitoftech.net/2015/03/31/asp-net-web-api-claims-authorization-with-asp-net-identity-2-1/的一个例子

Ste*_*eng 8

如果它在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);
}
Run Code Online (Sandbox Code Playgroud)

如果它在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);
}
Run Code Online (Sandbox Code Playgroud)