需要一个代码来检查VB中Asmx Web服务的每个请求和Web服务方法的Web服务标头上的有效密钥

时间:2019-03-30 10:24:49

标签: asp.net asmx

需要VB中的代码以通过asmx Web服务为每个Web服务请求和方法验证带有加密密钥的标头。 例如:通过将IHttpModule实现到具有异步BeginRequest事件的类, 我试图实现它不起作用。

在asp.net VB和框架4.6.1中将建议使用任何类似的示例。

-异步开始请求的以下代码在4.6 Framework中不起作用

public class BasicAuthHttpModule : IHttpModule  
{  
    const String AUTH_USER_PREFIX= "auth.user.";  
    static readonly IDictionary<String, String> logins = ConfigurationManager.AppSettings.AllKeys.Where(k => k.StartsWith(AUTH_USER_PREFIX))  
                                                        .ToDictionary(key => key.Replace(AUTH_USER_PREFIX, String.Empty), value => ConfigurationManager.AppSettings.Get(value));  
    public void Init(HttpApplication context)  
    {  
        context.AuthenticateRequest += OnAuthenticateRequest;  
        context.EndRequest += OnEndRequest;  
    }  


private static void OnAuthenticateRequest(object sender, EventArgs e)  
{  
    var authHeaders = HttpContext.Current.Request.Headers["Authorization"];  
    if (authHeaders != null)  
    {  
        var authHeadersValue = AuthenticationHeaderValue.Parse(authHeaders);  
        if (authHeadersValue.Scheme.Equals("basic", StringComparison.OrdinalIgnoreCase) && !String.IsNullOrWhiteSpace(authHeadersValue.Parameter))  
        {  

            try  
            {  
                var credentials = authHeadersValue.Parameter;  
                var encoding = Encoding.GetEncoding("iso-8859-1");  
                credentials = encoding.GetString(Convert.FromBase64String(credentials));  
                string name = credentials.Split(':').First();  
                string password = credentials.Split(':').Last();  

                if (logins.Any(l => l.Key.Equals(name) && l.Value.Equals(password)))  
                {  
                    //Set the principal for validated user  

                    var principal = new GenericPrincipal(new GenericIdentity(name), null);  
                    Thread.CurrentPrincipal = principal;  
                    if (HttpContext.Current != null)  
                    {  
                        HttpContext.Current.User = principal;  
                    }  
                }  
                else  
                {  
                    //Authentication failed  
                    HttpContext.Current.Response.StatusCode = 401;  
                }  
            }  
            catch (FormatException)  
            {  
                HttpContext.Current.Response.StatusCode = 401;  
            }  

        }  
    }  
}

0 个答案:

没有答案