webForumDet fria alternativet

session eller cookies i en webservice

.NET

4 svar · 445 visningar · startad av Addeladde

Medlem sedan jan. 20013 406 inlägg
Frågan#1

Hej

Jag har ett gigantist projekt som jag nu måste skriva lite webservices till.
Problemet är att projektet använder flera databaser och vilken databas som ska användas för respektive användare väljs när man loggar in. Sedan sparas ett namn som refererar till rätt databas i en cookie.

Nu när jag ska köra med webservices mot detta projekt så blir det problem. Hur kan jag lagra vilken databas som gäller när man ansluter med webservicen.

sessions verkar inte fungera och jag kan inte ställa in så att den lagrar cookieinformation i url:en.

Medlem sedan feb. 20013 023 inlägg
#2

Sessions, cookies ska fungera:
service1.CookieContainer = new System.Net.CookieContainer();

Medlem sedan jan. 20013 406 inlägg
#3

Gärna något lite större exempel. Idag har jag en klass som hanterar inställningscookien i projektet.

using System;
using System.Web;
using System.Web.Configuration;

/// <summary>
/// Class used to read and store user preferences for the application
/// </summary>

public class AppCookie
{
    public AppCookie()
    {
        if (HttpContext.Current.Request.Cookies[WebConfigurationManager.AppSettings["AppCookieName"]] == null)
        {
           this.PreferencesCookie = new HttpCookie(WebConfigurationManager.AppSettings["AppCookieName"]);
           this.PreferredTheme = string.Empty;
           this.DB = string.Empty;
           this.PreferredCompany = string.Empty;
            
        }

        else
        {
           this.PreferencesCookie = HttpContext.Current.Request.Cookies[WebConfigurationManager.AppSettings["AppCookieName"]];

        }
    }
    public void Save()
    {
        this.PreferencesCookie.Expires = DateTime.Now.AddDays(90.0);
        HttpContext.Current.Response.Cookies.Add(this.PreferencesCookie);

    }

    private HttpCookie _w;

    /// <summary>
    /// Gets or sets the preference cookie for the class
    /// </summary>

    private HttpCookie PreferencesCookie
    {
        get { return this._w; }
        set { this._w = value; }
    }

    /// <summary>
    /// Gets or sets the preferred theme for the user
    /// </summary>

    public string PreferredTheme
    {
        get { 
            if (this.PreferencesCookie["Theme"] != null)
                return this.PreferencesCookie["Theme"].ToString();
            else
                return "";
            }    
        set { this.PreferencesCookie["Theme"] = value; }
    }
    public string PreferredCompany
    {
        get {
            if (this.PreferencesCookie["Company"] != null)
                return this.PreferencesCookie["Company"].ToString();
            else
                return "";
            }    
        set { this.PreferencesCookie["Company"] = value; }
    }

    public string DB
    {
        get {
            if (this.PreferencesCookie["DB"] != null && this.PreferencesCookie["DB"] != "")
                return this.PreferencesCookie["DB"].ToString();
            else
            {
                if(HttpContext.Current.Session["db"] != null)
                    return HttpContext.Current.Session["db"].ToString();
                else
                    return string.Empty;
            }
            }
        set { this.PreferencesCookie["DB"] = value; }
    }

    public static string GetDB()
    {
        return (new AppCookie()).DB;
    }

    public static string GetCompany()
    {
        return (new AppCookie()).PreferredCompany;
    }

    

}

Sedan när jag ska ansluta till databasen så gör jag så här:

        DBAccess db = new DBAccess(ConfigurationManager.ConnectionStrings[AppCookie.GetDB()].ConnectionString);
Medlem sedan jan. 20013 406 inlägg
#4

Hej

Jag använder WCF och efter att ha googlat så verkar det inte fungera med Cookies. Skumt

What used to be a single step in Visual Studio 2005 is now three steps in Visual Studio 2008. The reason behind this is that Visual Studio 2008 wants you to use WCF for web service client code. We cannot use WCF client code as it does not provide as much control over the web request as the older web service proxy class did. In this article, we need some control over specifying the cookies when invoking the web service. This is not possible if we are using the WCF based client code, hence we have to use the older web service proxy.
Medlem sedan dec. 19996 522 inlägg
#5

WCF använder ju SecurityContextToken för hantering av Sessions, på två olika sätt beroende om det är i en webfarm-miljö eller inte. SecurityContextToken sparar innehållet nyckeln samt klientens identitet. Så sessioner kan du använda

251 ms totalt · 4 externa anrop · v20260731065814-full.e96017d9
123 ms — deklarationer (db)
0 ms — hämta statistik (cache)
125 ms — hämta tråd, inlägg och bilagor (db)
122 ms — ändringar (db)