如何使用用户名和密码作为凭据创建asmx Web服务?

时间:2017-01-05 09:45:32

标签: c# asp.net web-services asmx

我想创建webservice,将用户名和密码作为信用卡

我的班级是

public class UserDetails : System.Web.Services.Protocols.SoapHeader
    {
        public string userName { get; set; }
        public string password { get; set; }
       public bool IsValid()
        {
            //Write the logic to Check the User Details From DataBase  
            //i can chek with some hardcode details UserName=Nitin and Password=Pandit  
            return this.userName == "xxxxx" && this.password == "xxxxxxx";
            //it'll check the details and will return true or false   
        }

我的asmx文件是

public UserDetails User;
        [WebMethod]
        [SoapHeader("User", Required = true)]
        public void SayHello(string userName)
        {
            if (User != null)
            {
                if (User.IsValid())
                {
                    Context.Response.ContentType = "application/json; charset=utf-8";
                    Context.Response.Write("Hello... " + userName + "");
                }
                else
                {
                    Context.Response.ContentType = "application/json; charset=utf-8";
                    Context.Response.Write("user crediantials not valid");
                }
            }
            else
            {
                Context.Response.ContentType = "application/json; charset=utf-8";
                Context.Response.Write("Error in authentication");
            }
        }

什么时候iam呼叫服务不会使信任进入其他条件。 iam调用如下所示的服务但是我得到了错误响应。

HttpWebRequest request = HttpWebRequest.Create(Url.weburl + "SayHello?userName=pavan") as HttpWebRequest;
               // String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
                request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
                //  request.Credentials = new NetworkCredential("reload", "reload123");
                request.Method = "GET";





                WebResponse response = await request.GetResponseAsync();
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();

}

每当我调用服务用户值时总是null.how传递凭证。请帮助我。我是从最近2天开始搜索的。但是没有得到解决方案,请帮助我。

0 个答案:

没有答案