Throwing a XML Web Service Exception for invalid username / password

三世轮回 提交于 2020-01-06 05:59:46

问题


I've created a web service and am using a Soap header for authentication, as described here: http://aspalliance.com/805

I've adapted it, so that in every method, it calls a seperate "authenticate" method, which searches username and password in the db, and returns true or false. My question is, within this method, if it returns false (ie, the user isn't validated) how should i throw an exception, that filters back to the consumer application?


回答1:


First of all, you'd do better to add a Login operation that takes your username/password header as input, authenticates the user, then returns an authorization token of some kind in a return SOAP Header. This header should then be supplied as in input header in all subsequent operations.

Second, you should throw a SOAPException. This will translate more or less directly into a SOAP Fault. A SOAP Fault is the appropriate way to indicate an error with a web service operation for the same reason that Exceptions are better than return status in a normal method - you don't have to check the return status at the point of the call.

Finally, were you aware that Microsoft has declared ASMX web services to be "legacy" code, and that they are no longer fixing bugs in it? It's time to move to WCF.




回答2:


i have used soap exceptions for login fails:

[WebMethod]
    [SoapHeader("authentication")]
    public User Authenticate()
    {
        try
        {
            authentication.Roles = new string[] { UserRoles.Users };
            ConfigureAuthentication();
            Service<ISecurity>.Interface.Authenticate();
            Guid userId = Service<ISecurity>.Interface.GetUserId(authentication.UserName);
            return Service<IObjectRetriever>.Interface.Retrieve<User>(userId);
        }
        catch (Exception ex)
        {
            WriteException(ex);
            throw new SoapException(ex.Message, new XmlQualifiedName(SoapException.ServerFaultCode.Name), ex);
        }
    }


来源:https://stackoverflow.com/questions/1251578/throwing-a-xml-web-service-exception-for-invalid-username-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!