0
我有一个asp.net web服务和一个winforms应用程序。 winform应用程序调用Web服务的方法。如何在winform应用程序调用服务时设置cookie
我在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=\"Web\" /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应用程序:
public ServicesTinhLuong.ServicesTinhLuongSoapClient service = new ServicesTinhLuong.ServicesTinhLuongSoapClient();
场景: 当应用程序的WinForms调用Web服务方法将不得不通过登录满足登录hod(字符串用户名,字符串密码),成功登录web服务后存储cookie。然后WinForms应用程序可以调用的Web服务等方法而不用通过登录方法
谢谢你对我的帮助,但是这两种方式都不是我遇到的问题 – duonglb