الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مساعدة في كود Cookie

بدأه hossam.ra00 في 15 يوليو 2013 · 3 رد · 829 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

 

اريد ان اكمل هذا الكود لانة يطلع لية خطأ مش قادر اعرف الخطأ فين

 

بعد ازنكم

public class Utility
{

    public void CreateCookie(string CookieName,string[]Keys,string[]Values,bool Expired)
    {
        HttpCookie c = new HttpCookie(CookieName);
        if (Keys!= null)
        {
            for (int x = 0; x < Keys.Length; x++)
                c.Values.Add(Keys[x], Values[x]);
            if (!Expired)
                c.Expires = DateTime.Now.AddYears(5);
        }else
            c.Expires = DateTime.Now.AddYears(-5);
            res.Cookies.Add(c);
    }
    public string ReadFromCookie(string CookieName, String Key, HttpRequest req)
    {
        try
        {
            return req.Cookies[CookieName][Kye].ToString();
        }
        catch
        {
            return null;
        }

    }

    public void RemoveCookie(string CookieName, HttpResponse res)
    {
        CreateCookie(CookieName, null, null  , false  , res );
    }
}

شكرا لكم جميعا

تم تعديل هذه المشاركة بواسطة hossam.ra00 في 16 يوليو 2013 في 22:25

−1
#2

مفيش احد هنا للمساعدة

#3

ما هو الخطء عنك

#4

بسم الله الرحمن الرحيم 

 

اخي الكريم هذا مثال اشتغلتوا بمشروع التخرج :
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Windows.Forms;

/// <summary>
/// Summary description for PublicFunctions
/// </summary>
public class LoginFunctions
{
    private static DBClassesDataContext db = new DBClassesDataContext();
    private static HttpContext BR;
    public LoginFunctions()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    //Check If Account Exist In The DataBase
    public static bool CheckUser(string name, string pass)
    {
        var theaccount = from account in db.Accounts
                         where (account.UserName == name) && (account.PassWord == pass)
                         select account;
        if (theaccount.Count<Account>() > 0)
            return true;
        else
            return false;
    }
    public static AccountType GetType(string name)
    {
        var theaccount = from account in db.Accounts
                         where (account.UserName == name)
                         select account;
        if (theaccount.Count<Account>() > 0)
        {
            foreach (var i in theaccount)
            {
                return SetAccountType(((Account)i).Privilege);
            }
        }
        return AccountType.unknown;
    }
    // Convert String to AccountType
    public static AccountType SetAccountType(string type)
    {
        AccountType Result = AccountType.unknown;
        Result = (AccountType)Enum.Parse(typeof(AccountType), type);
        return Result;
    }
    //Remove Cookie
    public static void DeleteCookie()
    {
        BR = HttpContext.Current;
        //Give The Cookie Expired Expiration Date To Let The Browser Delete It
        if (BR.Request.Cookies["HMS_ACC"] != null)
        {
            HttpCookie myCookie = new HttpCookie("HMS_ACC");
            myCookie.Expires = DateTime.Now.AddDays(-1d);
            BR.Response.Cookies.Add(myCookie);
        }
    }
    //Create Session
    public static void CreateSession(string name, string pass)
    {
        BR = HttpContext.Current;
        BR.Session.Add("UserName", name);
        BR.Session.Add("Password", pass);
        BR.Session.Add("Privilege", GetType(name).ToString());
    }
    //Create Cookie
    public static void CreateCookie(string name, string pass)
    {
        BR = HttpContext.Current;
        //Create a new cookie, passing the name into the constructor
        HttpCookie cookie = new HttpCookie("HMS_ACC");
        cookie.Values.Add("UserName", name);
        cookie.Values.Add("Password", pass);
        cookie.Values.Add("Privilege", GetType(name).ToString());
        //Set the cookie to expire in 2 Weeks
        DateTime dtNow = DateTime.Now;
        TimeSpan tsMinute = new TimeSpan(14, 0, 0, 0);
        cookie.Expires = dtNow + tsMinute;

        //Add the cookie
        BR.Response.Cookies.Add(cookie);
    }
    //Assure if we can get in the page
    public static bool CanGetIn()
    {
        BR = HttpContext.Current;
        //Prepare Cookie If Exist
        HttpCookie cookie = BR.Request.Cookies["HMS_ACC"];
        //Session
        if (BR.Session.Count > 0)
        {
            if (!LoginFunctions.CheckUser(BR.Session["UserName"].ToString(), BR.Session["Password"].ToString()))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        //Cookie
        else if (cookie != null)
        {
            if (String.IsNullOrEmpty(cookie.Values[0].ToString()))
            {
                LoginFunctions.DeleteCookie();
                return false;
            }
            else
            {
                if (LoginFunctions.CheckUser(cookie.Values["UserName"].ToString(), cookie.Values["Password"].ToString()))
                {

                    LoginFunctions.CreateSession(cookie.Values["UserName"].ToString(), cookie.Values["Password"].ToString());
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        else
        {
            return false;
        }
    }
    //Get StructureID
    public static int GetStructureId(int AccountID)
    {
        var TheResult = from employ in db.Employees
                        where (employ.Account_ID == AccountID)
                        select new { theid = employ.Section_ID };
        foreach (var s in TheResult)
        {
            return s.theid;
        }
        return -1;
    }
    //Get Account ID
    public static int GetAccountID(string name)
    {
        var theaccount = from account in db.Accounts
                         where (account.UserName == name)
                         select new {theID = account.ID};
        foreach (var s in theaccount)
        {
            return s.theID;
        }
        return -1;
    }
}

طبعا جربتوا على مستخدم واحد لكن ماجربتوا على اكثر من مُستخدم بنفس الوقت.
ما دققت بالكود يلي حضرتك حطيتوا بس بحاول كمان شوي او بعد الفطور اقرأ الكود وأحللو إن شاء الله
وبأكد على كلام الأخ كريم ياريت تحط نص الخطأ يلي بيطلع عندك  
أتمنى ان أكون قد أفدت 
اخوك في الله أبو ياسر

تم تعديل هذه المشاركة بواسطة samer226047 في 19 يوليو 2013 في 19:19

إن كلماتنا ستبقى ميتة أعراساً من الشموع لا حراك فيها جامدة ، حتى إذا متنا من أجلها انتفضت حية وعاشت بين الأحياء ، كل كلمة عاشت كانت قد اقتاتت قلب إنسان حي ، فعاشت بين الأحياء ، والأحياء لا يتبنون الأموات

الليل ولى وجاء دورك يا صباح سفينة الإيمان سارت لا تبالي بالرياح

ولا تنسوني من دعائكم

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…