2017-05-02 3 views
0

Ich habe eine asp.net Web-Service und eine Winforms App. Die Winform-Anwendung ruft die Methoden des Web-Service auf.Wie setze ich Cookie, wenn winform Anwendung Anrufe Service

meinen Code in asp.net Webservice asmx Seite:

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> 

hinzufügen I Service Referenzen in WinForms App:

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

Szenario: Wenn die Anwendung WinForms rufen Web-Service-Methoden müssen sich über die Anmeldung anmelden hod (string Benutzername, string Passwort), nach erfolgreichem Login Web-Service zum Speichern von Cookies. dann kann WinForms App andere Methoden der Web-Services aufrufen wieder durch die Login-Methode

Antwort

0

zur Authentifizierung in Web-Service gibt es so viele Möglichkeiten, Verhaltensweisen ohne Anmeldung verwenden und sonst .... aber es gibt einige Links Whit Proben helfen

Sie

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

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

ich schlage vor, Sie seccend Link

wenn Ihre Sitzung sterben Benutzer brauchen


wieder

+0

Sie sich einloggen Vielen mir geholfen, aber beide Wege sind nicht das Problem, dass ich begegnet bin – duonglb

Verwandte Themen