簡體   English   中英

Winform應用程序調用服務時如何設置Cookie

[英]How to set cookie when winform application calls service

我有一個asp.net Web服務和一個winforms應用程序。 Winform應用程序調用Web服務的方法。

我在asp.net Web服務的asm​​x頁面中的代碼:

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應用程序中添加了Service References:

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

場景:當應用程序WinForms調用Web服務方法成功存儲Web cookie后,必須通過登錄方法(字符串用戶名,字符串密碼)登錄。 然后WinForms應用程序可以調用Web服務的其他方法,而無需通過登錄方法再次登錄

對於Web服務中的身份驗證,有很多使用行為的方式,其他....但是有一些鏈接可以幫助您了解示例

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

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

我建議你先鏈接

如果您的會話用戶需要登錄


再次

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM