根据登录信息C#饼干饼干、根据、信息

2023-09-04 04:54:23 作者:棒棒糖的约定

我有一个Web应用程序一个在默认情况下,一个在默认设置2 2登录控件(后,我得到它的工作的命名规则将​​被更新)。

我在做什么是设置在每次登录时,将发送一个ConnectionString的名字从登录控件验证方法的cookie。它发送一个字符串,它是很难codeD名为认证用户的基类。该类执行以下操作...

 公共类AuthenticatedUser:System.Web.UI.Page
{
公共静态字符串的ConnectionString
{
    得到
    {
        的HttpCookie的myCookie = HttpContext.Current.Request.Cookies [的connectionString];
        返回GetConnectionStringFromName(myCookie时);
    }
    组
    {
        的HttpCookie oldCookie = HttpContext.Current.Request.Cookies [的connectionString];
        oldCookie.Expires = DateTime.Now.AddDays(-1);
        的HttpCookie饼干=新的HttpCookie(的connectionString);
        cookie.Value =价值;
        cookie.Expires = DateTime.Now.AddYears(100);
        HttpContext.Current.Request.Cookies.Add(饼干);
        字符串VAL = cookie.Value;
    }
}

私人静态字符串GetConnectionStringFromName(的HttpCookie的myCookie)
{
    字符串的connectionStringName = myCookie.Value;
    返回ConfigurationManager.ConnectionStrings [的connectionStringName] .ConnectionString;
}
 

}

我seding在琴弦数据库1和的Database2这取决于登录他们使用的控制。当我通过code调试,将connectionString为将所有的cookie信息,一切的伟大工程的Database2,然而每次我登录时使用相关联的表数据库1它设置cookie,但是当得到的是被称为它仍然是引用的Database2

这是一个问题,因为饼干被命名相同,不覆盖海誓山盟或更新自己还是有我的code的一个问题?

编辑 - 它仍然不工作从上下文取出饼干,它仍然给了我的Database2当我运行数据库1

 公共静态字符串的ConnectionString
{
    得到
    {
        的HttpCookie的myCookie = HttpContext.Current.Request.Cookies [的connectionString];
        返回GetConnectionStringFromName(myCookie时);
    }
    组
    {
        如果(HttpContext.Current.Request.Cookies [的connectionString]!= NULL)
        {
            ExpireCookies(HttpContext.Current);
        }
        的HttpCookie饼干= HttpContext.Current.Response.Cookies [的connectionString];
        cookie.Value =价值;
        cookie.Expires = DateTime.Now.AddYears(100);
        HttpContext.Current.Response.Cookies.Add(饼干);
        字符串VAL = cookie.Value;
    }
}

私人静态字符串GetConnectionStringFromName(的HttpCookie的myCookie)
{
    字符串的connectionStringName = myCookie.Value;
    返回ConfigurationManager.ConnectionStrings [的connectionStringName] .ConnectionString;
}

私有静态无效ExpireCookies(HttpContext的电流)
{
    VAR allCookies = current.Request.Cookies.AllKeys;
    的foreach(在allCookies.Select VAR库克(C => current.Response.Cookies [C]),其中(煮=>!煮= NULL))
    {
        cook.Value =;
        cook.Expires = DateTime.Now.AddDays(-1);
        current.Response.Cookies.Remove(cook.Name);
    }
}
 
开启微信这个新功能,让你的隐私得到更好的保护

最后编辑它的工作原理......这里是工作code。如果有人有兴趣...

 公共静态字符串的ConnectionString
{
    得到
    {
        的HttpCookie的myCookie = HttpContext.Current.Request.Cookies [的connectionString];
        返回GetConnectionStringFromName(myCookie时);
    }
    组
    {
        如果(HttpContext.Current.Request.Cookies [的connectionString]!= NULL)
        {
            ExpireCookies(HttpContext.Current);
        }
        VAR allCookies = HttpContext.Current.Request.Cookies.AllKeys;
        的HttpCookie饼干=新的HttpCookie(的connectionString);
        cookie.Value =价值;
        cookie.Expires = DateTime.Now.AddYears(100);
        HttpContext.Current.Request.Cookies.Add(饼干);
        字符串VAL = cookie.Value;
    }
}

私人静态字符串GetConnectionStringFromName(的HttpCookie的myCookie)
{
    字符串的connectionStringName = myCookie.Value;
    返回ConfigurationManager.ConnectionStrings [的connectionStringName] .ConnectionString;
}

私有静态无效ExpireCookies(HttpContext的电流)
{
    VAR allCookies = current.Request.Cookies.AllKeys;
    的foreach(在allCookies.Select VAR库克(C => current.Response.Cookies [C]),其中(煮=>!煮= NULL))
    {
        cook.Value =;
        cook.Expires = DateTime.Now.AddDays(-1);
        current.Request.Cookies.Remove(cook.Name);
        cook.Name =;
    }
}
 

解决方案

您所需要的code(到期的cookie)的一部分。但你也需要从上下文中删除,你可以试试下面的:

 私有静态无效ExpireCookies(HttpContext的电流)
    {
        VAR allCookies = current.Request.Cookies.AllKeys;

        的foreach(在allCookies.Select VAR库克(C => current.Response.Cookies [C]),其中(煮=>!煮= NULL))
        {
            cook.Value =;
            cook.Expires = DateTime.Now.AddDays(-1);
            current.Response.Cookies.Remove(cook.Name);
        }

    }
 

I have 2 login controls on a web application one on default and one on default2 (the naming convention will be updated after I get it working).

What I am doing is setting a cookie on each login that will send a connectionstring name from the login controls authenticate method. It is sending a string that is hard coded to a base class called Authenticate Users. The class is doing the following...

public class AuthenticatedUser : System.Web.UI.Page
{
public static string ConnectionString
{
    get
    {
        HttpCookie myCookie = HttpContext.Current.Request.Cookies["connectionString"];
        return GetConnectionStringFromName(myCookie);
    }
    set
    {
        HttpCookie oldCookie = HttpContext.Current.Request.Cookies["connectionString"];
        oldCookie.Expires = DateTime.Now.AddDays(-1);
        HttpCookie cookie = new HttpCookie("connectionString");
        cookie.Value = value;
        cookie.Expires = DateTime.Now.AddYears(100);
        HttpContext.Current.Request.Cookies.Add(cookie);
        string val = cookie.Value;
    }
}

private static string GetConnectionStringFromName(HttpCookie myCookie)
{
    string connectionStringName = myCookie.Value;
    return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
}

}

I am seding in the strings "database1" and "database2" depending on which login control they use. When I debug through the code, the connectionstring is setting all of the cookie information and everything works great for "database2" however everytime I log in using the form associated with "database1" it sets the cookie but when the get is called it is still referencing "database2"

Is this an issue because the cookies are named the same and do not overwrite eachother or update themselves or is there a problem with my code?

edit -- it is still not working with removing the cookies from the context, it still gives me the "database2" when I run "database1"

 public static string ConnectionString
{
    get
    {
        HttpCookie myCookie = HttpContext.Current.Request.Cookies["connectionString"];
        return GetConnectionStringFromName(myCookie);
    }
    set
    {
        if (HttpContext.Current.Request.Cookies["connectionString"] != null)
        {
            ExpireCookies(HttpContext.Current);
        }
        HttpCookie cookie = HttpContext.Current.Response.Cookies["connectionString"];
        cookie.Value = value;
        cookie.Expires = DateTime.Now.AddYears(100);
        HttpContext.Current.Response.Cookies.Add(cookie);
        string val = cookie.Value;
    }
}

private static string GetConnectionStringFromName(HttpCookie myCookie)
{
    string connectionStringName = myCookie.Value;
    return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
}

private static void ExpireCookies(HttpContext current)
{
    var allCookies = current.Request.Cookies.AllKeys;
    foreach (var cook in allCookies.Select(c => current.Response.Cookies[c]).Where(cook => cook != null))
    {
        cook.Value = "";
        cook.Expires = DateTime.Now.AddDays(-1);
        current.Response.Cookies.Remove(cook.Name);
    }
} 

Final Edit It works...here is the working code if anyone is interested...

public static string ConnectionString
{
    get
    {
        HttpCookie myCookie = HttpContext.Current.Request.Cookies["connectionString"];
        return GetConnectionStringFromName(myCookie);
    }
    set
    {
        if (HttpContext.Current.Request.Cookies["connectionString"] != null)
        {
            ExpireCookies(HttpContext.Current);
        }
        var allCookies = HttpContext.Current.Request.Cookies.AllKeys;
        HttpCookie cookie = new HttpCookie("connectionString");
        cookie.Value = value;
        cookie.Expires = DateTime.Now.AddYears(100);
        HttpContext.Current.Request.Cookies.Add(cookie);
        string val = cookie.Value;
    }
}

private static string GetConnectionStringFromName(HttpCookie myCookie)
{
    string connectionStringName = myCookie.Value;
    return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
}

private static void ExpireCookies(HttpContext current)
{
    var allCookies = current.Request.Cookies.AllKeys;
    foreach (var cook in allCookies.Select(c => current.Response.Cookies[c]).Where(cook => cook != null))
    {
        cook.Value = "";
        cook.Expires = DateTime.Now.AddDays(-1);
        current.Request.Cookies.Remove(cook.Name);
        cook.Name = "";
    }
} 

解决方案

you have part of the code needed (expiring the cookie). but you also need to remove from the context, try something like the following:

private static void ExpireCookies(HttpContext current)
    {
        var allCookies = current.Request.Cookies.AllKeys;

        foreach (var cook in allCookies.Select(c => current.Response.Cookies[c]).Where(cook => cook != null))
        {
            cook.Value = "";
            cook.Expires = DateTime.Now.AddDays(-1);
            current.Response.Cookies.Remove(cook.Name);
        }

    }

 
精彩推荐
图片推荐