2

这是我的控制器的一些示例代码:

public class ContentController : ApiController
{
    private IContentService _contentService;

    public ContentController(
        IContentService contentService)
    {
        _contentService = contentService;
    }

    // GET api/Content/5
    [HttpGet]
    [ActionName("GetContent")]
    public Content GetContent(int Id)
    {
        Content content = _uow.Contents.GetById(Id);
        if (content == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }
        return content;
    }

我想找到用户的ID。有人能告诉我我该怎么做吗?我正在使用标准的 ASP.NET 简单身份验证,并且我使用了 ASP.NET MVC 帐户控制器来登录我的用户。

4

2 回答 2

2

由于您使用的是表单身份验证,您可以尝试使用HttpContext类和Identity属性:

var currentUser = HttpContext.User.Identity;
于 2013-07-09T08:21:24.077 回答
0

在 api 控制器中,您可以简单地使用:

User.Identity.Name  

检查

User.Identity.IsAuthenticated

第一的。

于 2013-07-09T08:23:06.200 回答