【问题标题】:ASP.NET MVC authentication using http header使用 http 标头的 ASP.NET MVC 身份验证
【发布时间】:2014-01-14 15:16:13
【问题描述】:

在我的 ASP.NET MVC 4 应用程序中,我需要使用包含用户名的现有 http 标头值 对用户进行身份验证。

它使用表单身份验证工作:当用户尚未通过身份验证时,他将被重定向到登录页面。登录页面的控制器检查 httpHeader 值并使用FormsAuthentication.SetAuthCookie(username,false) 连接用户并将用户重定向到我的应用程序的主页。

但我想避免这种重定向,并在触发“用户未通过身份验证”事件时直接尝试对用户进行身份验证。

轴:

  • FormsAuthentication_OnAuthenticate
  • 覆盖 AuthorizeAttribute
  • 验证请求

【问题讨论】:

    标签: asp.net asp.net-mvc authentication cookies http-headers


    【解决方案1】:

    如果我理解正确,那么您只需使用用户名设置 currentPrincipal

    Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(user, "Basic"),                                     new string[] {}); 
    

    所以从现在开始它就可以工作了,你也可以访问用户 User.Identity.Name

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 2010-09-24
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 2021-04-28
      相关资源
      最近更新 更多