如何在winform应用程序调用服务时设置cookie

时间:2017-05-02 18:16:13

标签: c# asp.net winforms cookies

我有一个asp.net网络服务和一个winforms应用程序。 winform应用程序调用Web服务的方法。

我在asp.net web服务asmx页面中的代码:

namespace ServicesTinhLuong
{
    public class ServicesTinhLuong : System.Web.Services.WebService
    {
        #region Login Webservice
        [WebMethod(Description = "Login Webservice")]

        public bool LogIn(string username, string password)
        {
            try
            {
                var usernameCF = WebConfigurationManager.AppSettings["LogInServiceUser"];
                var passCF = WebConfigurationManager.AppSettings["LogInServicePass"];
                passCF = new Common().EncPwd(passCF);
                var loginSuccess = (username == usernameCF) && (password == passCF);
                if (loginSuccess)
                {
                    FormsAuthentication.SetAuthCookie(username, true);
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        [WebMethod(Description = "Logout Webservice")]
        [PrincipalPermission(SecurityAction.Demand, Authenticated = true)]
        public bool LogOut()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
                FormsAuthentication.SignOut();
            return true;
        }
        #endregion
        [WebMethod]
        [PrincipalPermission(SecurityAction.Demand, Authenticated = true)]
        public bool checkLogin(string username, string password)
        {
            try
            {
                SqlCommand command = new SqlCommand("CheckLogin", connection)
                {
                    CommandType = CommandType.StoredProcedure
                };
                command.Parameters.AddWithValue("@UserName", username);
                command.Parameters.AddWithValue("@Pass", password);

                if (connection.State == ConnectionState.Closed) connection.Open();
                DataTable dt = new DataTable();
                using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                {
                    adapter.Fill(dt);
                    command.Dispose();
                }
                connection.Close();
                if (dt.Rows.Count > 0) return true;
                else return false;
            }
            catch (Exception ex)
            {
                return false;
            }
            }
      }
}

的web.config

<configuration>
  <connectionStrings>
    <add name="ServerName" connectionString="DUONG-PC" />
    <add name="DatabaseName" connectionString="TinhLuong" />
    <add name="UserID" connectionString="sa" />
    <add name="Password" connectionString="sa123" />
  </connectionStrings>
  <appSettings>
    <add key="LuongCoBan" value="1120000" />
    <add key="LogInServiceUser" value="duonglb" />
    <add key="LogInServicePass" value="79958" />
  </appSettings>
  <system.web>
    <authentication mode="Forms">
      <forms name="AuthCookie" path="/" cookieless="UseCookies"></forms>
    </authentication>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
    </httpModules>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
      <remove name="ApplicationInsightsWebTracking"/>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler"/>
    </modules>
  </system.webServer>
</configuration>

我在winforms app中添加服务引用:

public ServicesTinhLuong.ServicesTinhLuongSoapClient service = new ServicesTinhLuong.ServicesTinhLuongSoapClient();

情形: 当应用程序WinForms调用Web服务方法时,必须通过登录方法(字符串用户名,字符串密码)登录,成功登录Web服务后才能存储cookie。然后WinForms应用程序可以调用其他Web服务方法,而无需通过登录方法再次登录

1 个答案:

答案 0 :(得分:0)

在Web服务中进行身份验证有很多方法可以使用行为等等.... 但有一些链接,惠特样本会帮助你

https://www.codeproject.com/Articles/9348/Web-Service-Authentication

https://www.codeproject.com/Articles/4398/Authentication-for-Web-Services-using-SOAP-headers

我建议你转发链接

如果您的会话死亡用户需要登录

再次