Re: ChatProvider not initialized

  •  05-20-2010, 7:41 PM

    Re: ChatProvider not initialized

    Here is the global.asax

     

    <%@ Application Language="C#" ClassName="GlobalAsax" %>

    <%@ Import Namespace="CuteChat"%>

    <%@ Import Namespace="System.Data"%>

    <%@ Import Namespace="System.Data.Common"%>

    <script runat=server>

    public override void Init()

    {

    base.Init();

    lock(typeof(CuteChat.ChatSystem))

    {

    if(!CuteChat.ChatSystem.HasStarted)

    {

    CuteChat.ChatProvider.Instance=new ExampleProvider();

    CuteChat.ChatSystem.Start(new CuteChat.AppSystem());

    }

    }

    }

    public class ExampleProvider : ChatProvider

    {

    static string PasswordField="Password";

    static string LowerNickName="LOWER(NickName)";

    static string LowerLoginName="LOWER(LoginName)";

    static ExampleProvider()

    {

    if(System.Configuration.ConfigurationSettings.AppSettings["DatabaseType"]=="MSAccess")

    {

    PasswordField="[Password]";

    LowerNickName="NickName";

    LowerLoginName="LoginName";

    }

    }

     

     

    static string SqlEncode(string value)

    {

    if(value==null)return "NULL";

    return "'"+value.ToString().Replace("'","''")+"'";

    }

     

    #region //TODO:integration code for friend list:

    // public override AppDataManager CreateDataManagerInstance(AppPortal portal)

    // {

    // return new MyDataManager(portal);

    // }

    //

    // class MyDataManager : AppDataManager

    // {

    // public override void AddContact(ChatIdentity identity, string userid)

    // {

    // string myname=ChatProvider.Instance.FromUserId(identity.UniqueId);

    // string friendname=ChatProvider.Instance.FromUserId(userid);

    // MyCommunityDatabase.AddFriend(myname);

    // this.OnContactAdded(identity,userid,friendname);

    // }

    // public override void RemoveContact(ChatIdentity identity, string userid)

    // {

    // //get the username from cutechat's userid

    // string myname=ChatProvider.Instance.FromUserId(identity.UniqueId);

    // string friendname=ChatProvider.Instance.FromUserId(userid);

    //

    // //my custom implementation

    // MyCommunityDatabase.RemoveFriend(myname,friendname);

    //

    // //tell cutechat that the relative have been removed.

    // this.OnContactRemoved(identity,userid);

    // }

    // public override IChatUserInfo[] GetContacts(ChatIdentity identity)

    // {

    // string myname=ChatProvider.Instance.FromUserId(identity.UniqueId);

    // string[] friends=MyCommunityDatabase.GetFriends(myname);

    // IChatUserInfo[] arr=new IChatUserInfo[friends.Length];

    // for(int i=0;i<friends.Length;i++)

    // {

    // string friendid=ChatProvider.Instance.ToUserId(friends[i]);

    // arr[i]=base.GetUserInfo(friendid);

    // }

    // return arr;

    // }

    //

    // public override void AddIgnore(ChatIdentity identity, string userid)

    // {

    // base.AddIgnore (identity, userid);

    // }

    // public override void RemoveIgnore(ChatIdentity identity, string userid)

    // {

    // base.RemoveIgnore (identity, userid);

    // }

    // public override IChatUserInfo[] GetIgnores(ChatIdentity identity)

    // {

    // return base.GetIgnores (identity);

    // }

    //

    //

    // public MyDataManager(AppPortal portal):base(portal)

    // {

    // }

    // }

    #endregion

    public override string GetConnectionString()

    {

    if(System.Configuration.ConfigurationSettings.AppSettings["DatabaseType"]=="MSAccess")

    {

    string accessfile=System.Configuration.ConfigurationSettings.AppSettings["MSAccessFilePath"];

    accessfile=System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,accessfile);

    return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+accessfile+";User Id=Admin;Password=;";

    }

    return System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

    }

    /// <summary>

    /// Get the information of the current user

    /// </summary>

    public override AppChatIdentity GetLogonIdentity()

    {

    HttpContext context=HttpContext.Current;

    if(context!=null)

    {

    if(context.User.Identity.IsAuthenticated)

    {

    string loginName=context.User.Identity.Name;

    string cachekey="NickName:"+loginName;

    string userid=ToUserId(loginName);

    string nickName=null;

    bool exists=GetUserInfo(loginName,ref nickName);

    if(!exists)

    return null;

     

    return new AppChatIdentity(nickName,false,userid,context.Request.UserHostAddress);

    }

    }

    return null;

    }

    /// <summary>

    /// find the username by the displayname

    /// </summary>

    public override string FindUserLoginName(string nickName)

    {

    if(nickName==null)return null;

     

    using(IDbConnection conn=CreateConnection())

    {

    conn.Open();

    using(IDbCommand cmd=conn.CreateCommand())

    {

    cmd.CommandText="SELECT LoginName FROM SampleUsers WHERE "+LowerNickName+"="+SqlEncode(nickName.ToLower());

    using(IDataReader sdr=cmd.ExecuteReader())

    {

    if(sdr.Read())

    {

    return sdr.GetString(0);

    }

    }

    }

    }

     

    return null;

    }

    /// <summary>

    /// get the information from the user

    /// This function is very important and be called very frequently.

    /// </summary>

    public override bool GetUserInfo(string loginName, ref string nickName, ref bool isAdmin)

    {

    string password=null;

    return GetUserInfo(loginName,ref nickName,ref password,ref isAdmin);

    }

    /// <summary>

    /// validate the user , and set the cookie

    /// </summary>

    public override bool ValidateUser(string loginName, string pwd)

    {

    string nickName=null;

    string password=null;

    bool isAdmin=false;

    bool exists=GetUserInfo(loginName,ref nickName,ref password,ref isAdmin);

    if(!exists)return false;

    if(password!=pwd)return false;

    System.Web.Security.FormsAuthentication.SetAuthCookie(loginName,false,"/");

    return true;

    }

     

     

    /// <summary>

    /// This function is very important and be called very frequently.

    /// </summary>

    public static bool GetUserInfo(string loginName,ref string nickName,ref string password,ref bool isAdmin)

    {

    loginName=loginName.ToLower();

    Hashtable table=(Hashtable)System.Web.HttpRuntime.Cache["UserInfo:"+loginName];

    if(table==null)

    {

    using(System.Data.IDbConnection conn=Instance.CreateConnection())

    {

    conn.Open();

    using(System.Data.IDbCommand cmd=conn.CreateCommand())

    {

    cmd.CommandText="SELECT * FROM SampleUsers WHERE "+LowerLoginName+"="+SqlEncode(loginName);

    using(System.Data.IDataReader sdr=cmd.ExecuteReader())

    {

    if(!sdr.Read())

    return false;

    table=new Hashtable(new CaseInsensitiveHashCodeProvider(),new CaseInsensitiveComparer());

    for(int i=0;i<sdr.FieldCount;i++)

    table[sdr.GetName(i)]=sdr.IsDBNull(i)?null:sdr.GetValue(i);

    }

    }

    }

    System.Web.HttpRuntime.Cache["UserInfo:"+loginName]=table;

    }

    nickName=table["NickName"].ToString();

    password=table["Password"].ToString();

    isAdmin=(1==Convert.ToInt32(table["IsAdmin"]));

    return true;

    }

     

    public static void CreateUser(string loginName,string nickname,string password)

    {

    using(System.Data.IDbConnection conn=Instance.CreateConnection())

    {

    conn.Open();

    using(System.Data.IDbCommand cmd=conn.CreateCommand())

    {

    cmd.CommandText="INSERT INTO SampleUsers (LoginName,NickName,"+PasswordField+",Email,IsAdmin) VALUES ("+SqlEncode(loginName)+","+SqlEncode(nickname)+","+SqlEncode(password)+",'',0)";

    cmd.ExecuteNonQuery();

    }

    }

     

    loginName=loginName.ToLower();

    System.Web.HttpRuntime.Cache.Remove("UserInfo:"+loginName);

    }

    public static void DeleteUser(string loginName)

    {

    using(IDbConnection conn=Instance.CreateConnection())

    {

    conn.Open();

    using(IDbCommand cmd=conn.CreateCommand())

    {

    cmd.CommandText="DELETE FROM SampleUsers WHERE "+LowerLoginName+"="+SqlEncode(loginName.ToLower());

    cmd.ExecuteNonQuery();

    }

    }

    loginName=loginName.ToLower();

    System.Web.HttpRuntime.Cache.Remove("UserInfo:"+loginName);

    }

    public static DataTable LoadUsers()

    {

    DataTable table=new DataTable("Users");

    using(IDbConnection conn=Instance.CreateConnection())

    {

    conn.Open();

    using(IDbCommand cmd=conn.CreateCommand())

    {

    cmd.CommandText="SELECT * FROM SampleUsers";

    using(IDataReader reader=cmd.ExecuteReader())

    {

    for(int i=0;i<reader.FieldCount;i++)

    {

    table.Columns.Add(reader.GetName(i),reader.GetFieldType(i));

    }

    object[] vals=new object[reader.FieldCount];

    while(reader.Read())

    {

    reader.GetValues(vals);

    table.Rows.Add(vals);

    }

    }

    }

    }

    table.AcceptChanges();

    return table;

    }

    public static void UpdateNickname(string loginName,string nickName)

    {

    loginName=loginName.ToLower();

    using(System.Data.IDbConnection conn=Instance.CreateConnection())

    {

    conn.Open();

    using(System.Data.IDbCommand cmd=conn.CreateCommand())

    {

    cmd.CommandText="UPDATE SampleUsers SET NickName="+SqlEncode(nickName)+" WHERE LOWER(LoginName)="+SqlEncode(loginName);

    cmd.ExecuteNonQuery();

    }

    }

    System.Web.HttpRuntime.Cache.Remove("UserInfo:"+loginName);

    }

    public static void UpdatePassword(string loginName,string password)

    {

    loginName=loginName.ToLower();

    using(System.Data.IDbConnection conn=Instance.CreateConnection())

    {

    conn.Open();

    using(System.Data.IDbCommand cmd=conn.CreateCommand())

    {

    cmd.CommandText="UPDATE SampleUsers SET "+PasswordField+"="+SqlEncode(password)+" WHERE "+LowerLoginName+"="+SqlEncode(loginName);

    cmd.ExecuteNonQuery();

    }

    }

    System.Web.HttpRuntime.Cache.Remove("UserInfo:"+loginName);

    }

     

    public static void UpdateIsAdmin(string loginName,bool isAdmin)

    {

    loginName=loginName.ToLower();

    using(IDbConnection conn=Instance.CreateConnection())

    {

    conn.Open();

    using(IDbCommand cmd=conn.CreateCommand())

    {

    cmd.CommandText="UPDATE SampleUsers SET IsAdmin="+(isAdmin?1:0)+" WHERE "+LowerLoginName+"="+SqlEncode(loginName);

    cmd.ExecuteNonQuery();

    }

    }

    System.Web.HttpRuntime.Cache.Remove("UserInfo:"+loginName);

    }

    }

     

    </script>
     
     
     

    chat provider

    I call it using this.  Not sure what you all need for this portion.  I used the stand alone demo.

    <script src='<%=ResolveUrl("~/CuteSoft_Client/CuteChat/")%>Support-Image-Button.js.aspx'></script>

     
     
View Complete Thread