【问题标题】:Custom WebAPI Authentication - How to authenticate HttpContext.Current.Request.LogonUserIdentity.Name自定义 WebAPI 身份验证 - 如何对 HttpContext.Current.Request.LogonUserIdentity.Name 进行身份验证
【发布时间】:2019-11-01 17:12:15
【问题描述】:

我有一个 webapi,它将存在于公司网络上,并且只有经过 Windows 身份验证的用户。

我正在尝试直接对 HttpContext.Current.Request.LogonUserIdentity.Name 进行身份验证,因为 HttpContext.Current.Request.LogonUserIdentity.IsAuthenticated 返回 false。

我这样做是为了避免非管理员用户的用户登录弹出窗口。

using System;
using System.Diagnostics;
using System.Web.Http;
using System.Web;
using System.Web.Http.Controllers;

namespace myAPI.Helpers
{

    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
    public class AuthorizeCustomAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(HttpActionContext actionContext)
        {

            // HttpContext.Current.User.Identity.Name is always empty at this point
            // and must authenticate first with HandleUnauthorizedRequest(actionContext)
            // but that pops up an annoying login screen,
            // HttpContext.Current.Request.LogonUserIdentity.Name has the value I need
            // but it is not authenticated which raises some security concerns

            // Check against a list of admins
            if (HttpContext.Current.Request.LogonUserIdentity.IsAuthenticated && Authentication.IsAdmin( HttpContext.Current.Request.LogonUserIdentity.Name ))
            {
                Debug.WriteLine("USER IS AUTHORIZED");
            }
            else
            {
                Debug.WriteLine("USER IS DENIED");
                //HandleUnauthorizedRequest(actionContext); // This will popup a login unless it is overridden
                actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.OK); // return a blank response instead
            }

        }
    }
}

【问题讨论】:

    标签: c# asp.net-web-api windows-authentication asp.net-authorization


    【解决方案1】:

    这是我最简单的解决方案:

    • 仅检查已知管理员的身份验证
    • 重定向未通过身份验证的管理员
    • 非管理员不会收到登录弹出窗口
    using System;
    using System.Diagnostics;
    using System.Web.Http;
    using System.Web;
    using System.Web.Http.Controllers;
    
    namespace myAPI.Helpers
    {
    
        [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
        public class AuthorizeCustomAttribute : AuthorizeAttribute
        {
            public override void OnAuthorization(HttpActionContext actionContext)
            {
    
                // Check against a list of admins
                if (Authentication.IsAdmin(HttpContext.Current.User.Identity.Name) || Authentication.IsAdmin( HttpContext.Current.Request.LogonUserIdentity.Name ))
                {
                    if(HttpContext.Current.User.Identity.IsAuthenticated || HttpContext.Current.Request.LogonUserIdentity.IsAuthenticated )
                    {
                        Debug.WriteLine("USER IS AUTHORIZED");
                    } else
                    {
                        Debug.WriteLine("USER IS AN ADMIN BUT IS UNAUTHENTICATED");
                        HandleUnauthorizedRequest(actionContext); // redirect to get authenticated
                    }
    
                }
                else
                {
                    Debug.WriteLine("USER IS NOT AN ADMIN AND IS DENIED");
                    actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.OK); // return a blank response
                }
    
            }
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 2022-12-10
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      • 2013-11-19
      • 2015-09-23
      • 2016-08-04
      相关资源
      最近更新 更多