Alternative to use HttpContext in System.Web for Owin

Xavier Egea

ASP.NET authentication is now based on OWIN middleware that can be used on any OWIN-based host. ASP.NET Identity does not have any dependency on System.Web.

I have an AuthorizeAttribute filter where I need to get the current user and add some properties to be retrieved later by action controllers.

The problem is that I have to use the HttpContext which belongs to System.Web. Is there any alternative of HttpContext for Owin?

public class WebApiAuthorizeAttribute : AuthorizeAttribute 
{
    public override async Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
    {
        base.OnAuthorization(actionContext);

        Guid userId = new Guid(HttpContext.Current.User.Identity.GetUserId());

        ApplicationUserManager manager = new ApplicationUserManager(new ApplicationUserStore(new ApplicationDbContext())) { PasswordHasher = new CustomPasswordHasher() };
        ApplicationUser user = await manager.FindByIdAsync(userId);

        actionContext.Request.Properties.Add("userId", user.LegacyUserId);
    }
}

Note: I found this question, which seems a duplicate, but asks for a solution working for NancyFx project, which is not valid for me.

Xavier Egea

This article gives me the solution:

Web API 2 introduced a new RequestContext class that contains a Principal property. This is now the proper location to look for the identity of the caller. This replaces the prior mechanisms of Thread.CurrentPrincipal and/or HttpContext.User. This is also what you would assign to if you are writing code to authenticate the caller in Web API.

So just modifying the line:

Guid userId = new Guid(HttpContext.Current.User.Identity.GetUserId());

by

Guid userId = new Guid(actionContext.RequestContext.Principal.Identity.GetUserId());

now, the reference to System.Web is not needed anymore.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Owin的System.Web中使用HttpContext的替代方法

来自分类Dev

Autofac OWIN TestServer和HttpContext

来自分类Dev

'System.Web.HttpContext'不包含'GetOwinContext'的定义

来自分类Dev

System.Web.HttpContext.Current在异步中线程安全吗?

来自分类Dev

'System.Web.HttpContext'不包含'GetOwinContext'之谜的定义

来自分类Dev

将Microsoft.AspNetCore.Http.HttpContext转换为System.Web.HttpContext

来自分类Dev

将System.Web.HttpContext.Current转换为System.Web.HttpContextBase

来自分类Dev

System.Web.Http.Owin WebApi2安装失败

来自分类Dev

System.Web.Http.WebHost中的GlobalConfiguration是否与Owin兼容?

来自分类Dev

System.Web.Http.WebHost中的GlobalConfiguration是否与Owin兼容?

来自分类Dev

OWIN和HttpContext.Current.Session

来自分类Dev

HttpContext.Current.Session为null + OWIN

来自分类Dev

非静态字段,方法或属性“ System.Web.Mvc.Controller.HttpContext.get”需要对象引用。

来自分类Dev

在哪里可以找到System.Web.HttpContext.Current.Server dll?

来自分类Dev

Ninject.MVC3-NinjectWebCommon RegisterServices System.Web.HttpContext.Current为null

来自分类Dev

System.Web.HttpContext.Current.Cache在不同的浏览器中返回不同的结果

来自分类Dev

在ASP.NET Core3.1中使用包含'System.Web.HttpContext'的旧项目

来自分类Dev

Ninject.MVC3-NinjectWebCommon RegisterServices System.Web.HttpContext.Current为null

来自分类Dev

将HttpContext.Session与use.Session()一起使用时,System.InvalidOperationException 组

来自分类Dev

无法加载文件或程序集“ System.Web.Http.Owin”

来自分类Dev

调用Microsoft.Owin.Cors后无法加载System.Web.Cors程序集

来自分类Dev

使用Ninject OWINHost的OWIN自托管应用是否需要system.web?

来自分类Dev

Owin/System.Web.Http.SelfHost - 迁移到 .NET Core 后无法启动

来自分类Dev

COM可见.NET类在经典ASP中使用时无法获取System.Web.HttpContext.Current

来自分类Dev

owin cors或web api cors

来自分类Dev

使用 MOQ 在 web api 中模拟 HttpContext

来自分类Dev

MVC 5,Web API和Owin

来自分类Dev

Ninject与Web Api,SignalR,MVC和OWIN

来自分类Dev

自托管(OWIN)Web API中的区域

Related 相关文章

  1. 1

    在Owin的System.Web中使用HttpContext的替代方法

  2. 2

    Autofac OWIN TestServer和HttpContext

  3. 3

    'System.Web.HttpContext'不包含'GetOwinContext'的定义

  4. 4

    System.Web.HttpContext.Current在异步中线程安全吗?

  5. 5

    'System.Web.HttpContext'不包含'GetOwinContext'之谜的定义

  6. 6

    将Microsoft.AspNetCore.Http.HttpContext转换为System.Web.HttpContext

  7. 7

    将System.Web.HttpContext.Current转换为System.Web.HttpContextBase

  8. 8

    System.Web.Http.Owin WebApi2安装失败

  9. 9

    System.Web.Http.WebHost中的GlobalConfiguration是否与Owin兼容?

  10. 10

    System.Web.Http.WebHost中的GlobalConfiguration是否与Owin兼容?

  11. 11

    OWIN和HttpContext.Current.Session

  12. 12

    HttpContext.Current.Session为null + OWIN

  13. 13

    非静态字段,方法或属性“ System.Web.Mvc.Controller.HttpContext.get”需要对象引用。

  14. 14

    在哪里可以找到System.Web.HttpContext.Current.Server dll?

  15. 15

    Ninject.MVC3-NinjectWebCommon RegisterServices System.Web.HttpContext.Current为null

  16. 16

    System.Web.HttpContext.Current.Cache在不同的浏览器中返回不同的结果

  17. 17

    在ASP.NET Core3.1中使用包含'System.Web.HttpContext'的旧项目

  18. 18

    Ninject.MVC3-NinjectWebCommon RegisterServices System.Web.HttpContext.Current为null

  19. 19

    将HttpContext.Session与use.Session()一起使用时,System.InvalidOperationException 组

  20. 20

    无法加载文件或程序集“ System.Web.Http.Owin”

  21. 21

    调用Microsoft.Owin.Cors后无法加载System.Web.Cors程序集

  22. 22

    使用Ninject OWINHost的OWIN自托管应用是否需要system.web?

  23. 23

    Owin/System.Web.Http.SelfHost - 迁移到 .NET Core 后无法启动

  24. 24

    COM可见.NET类在经典ASP中使用时无法获取System.Web.HttpContext.Current

  25. 25

    owin cors或web api cors

  26. 26

    使用 MOQ 在 web api 中模拟 HttpContext

  27. 27

    MVC 5,Web API和Owin

  28. 28

    Ninject与Web Api,SignalR,MVC和OWIN

  29. 29

    自托管(OWIN)Web API中的区域

热门标签

归档