0

嗨,我在一些身份验证和创建数据库项目时遇到了这个问题。在文档中,它说应该在创建之前调用身份验证。所以这是我完成的代码,我想知道我的事件顺序是否错误。

private void ApplicationBarIconButton_Click_5(object sender, EventArgs e)
    {
        Gateway.AuthenticateAsync("username", "password1", "username2", "password2");
        Gateway.AuthenticateCompleted += new EventHandler<ServiceReference.AuthenticateCompletedEventArgs>(AuthenticateTime);    
    }

    private DateTime _nestedDateStart;
    private DateTime _nestedDateEnd;
    private DateTime _nestedDateStartBreak1;
    private DateTime _nestedDateEndBreak1;
    private DateTime _nestedDateStartBreak2;
    private DateTime _nestedDateEndBreak2;

    ServiceReference.TimereportDto Timereport = new ServiceReference.TimereportDto();

    void AuthenticateTime(object sender, ServiceReference.AuthenticateCompletedEventArgs e)
    {

        Gateway.AuthenticateAsync("username1", "password1", "username2", "password2");     

        Timereport.Started = _nestedDateStart;
        Timereport.Ended = _nestedDateEnd;

        Timereport.Break1Start = _nestedDateStartBreak1;
        Timereport.Break1End = _nestedDateEndBreak1;

        Timereport.Break2Start = _nestedDateStartBreak2;
        Timereport.Break2End = _nestedDateEndBreak2;

        Timereport.Comment = Notes.Text;
        Timereport.EmployeeSignature = "apptest";

        Gateway.CreateTimereportAsync(Timereport,"ABD");
        Gateway.CreateTimereportCompleted += new EventHandler<ServiceReference.CreateTimereportCompletedEventArgs>(CreateTimereportCompleted);

    }

    void CreateTimereportCompleted(object sender, ServiceReference.CreateTimereportCompletedEventArgs e)
    {

    }

当我在“CreateTimereportCompleted”上设置断点时,出现错误,如下图所示:

在此处输入图像描述

如您所见,它返回消息“访问被拒绝,请先登录”。所以因为用户名和密码是正确的,所以我认为我的代码必须是错误的顺序或什么的。

更新

Gateway 是一个如下所示的服务引用:

ServiceReference.GatewaySoapClient Gateway = new ServiceReference.GatewaySoapClient();

如果身份验证 cookie 应该传递给我不知道的下一个服务调用。文档中没有任何说明。

他们在 Authentication 下面的文档中有一个 CookieContainer 但不是只有当你为 webbrowser 制作它时?

谁能帮助我?

4

1 回答 1

1

方法1:-(没有通过代码强行传递cookie)

在您的 ASMX Web 配置中添加 aspNetCompatibilityEnabled="true" 并设置 AllowCookies=false

在您的 ServiceReferences.ClientConfig 添加 AllowCookieContainer=true

方法2:-(通过代码传递cookie)

在您的 ASMX Web 配置中设置 AllowCookies=true 在您的 ServiceReferences.ClientConfig 添加 AllowCookieContainer=true 并且您可以设置

client.CookieContainer=yourCookieContainerVariable

并将这个“yourCookieContainerVariable”传递给下一个服务调用。

当您有单独的 url 用于身份验证和其他业务功能时,此方法特别有用

如图所示:http ://www.kotancode.com/2010/08/06/aspnet-authentication-wp7/

于 2013-05-21T13:03:04.833 回答