2013-10-10 9 views
-1

,我在如何解决这个错误无法投类型“System.Int32”的对象使用窗体身份验证为我的网站键入“System.String”

if (reader1.Read()) 
     { 
      FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
       1, 
       tbDomainID.Text, 
       DateTime.Now, 
       DateTime.Now.AddMinutes(30), 
       true, 
       role = reader1.GetInt64(0),// this line 
       FormsAuthentication.FormsCookiePath); 
      string hash = FormsAuthentication.Encrypt(ticket); 
      HttpCookie cookie = new HttpCookie(
      FormsAuthentication.FormsCookieName, 
      hash); 
      if (ticket.IsPersistent) cookie.Expires = ticket.Expiration; 
      Response.Cookies.Add(cookie); 

得到一个错误,当我将其转换GetString(0)GetInt64(0)在会告诉我另一个错误

无法隐式转换类型“长”到“字符串”

任何一个可以告诉我在哪里我一个我错了,或者我该怎么做。

我从我的数据库中获取整数值。

+0

角色期待一个字符串。你给它一个Int64(即很长) – Liam

+0

如何给角色一个整数值 – amitesh

+0

我写了3条评论,但说实话,单词让我失望.... *投票关闭* ... – Liam

回答

1

这是2nd constructor overload上的FormsAuthenticationTicket Class上的UserData参数。

正如你所看到的,这个字符串和reader1.GetInt64(0)将返回一个long

解决这个问题的方法就是打电话给.ToString()像这样:

role = reader1.GetInt64(0).ToString(),// this line 

虽然,我不知道我喜欢的方式你设置在同一时间,你传递变量它给构造函数。我个人会这样做:

if (reader1.Read()) 
{ 
    role = reader1.GetInt64(0).ToString(); 

    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
     1, 
     tbDomainID.Text, 
     DateTime.Now, 
     DateTime.Now.AddMinutes(30), 
     true, 
     role, 
     FormsAuthentication.FormsCookiePath); 

    string hash = FormsAuthentication.Encrypt(ticket); 
    HttpCookie cookie = new HttpCookie(
     FormsAuthentication.FormsCookieName, 
     hash); 

    if (ticket.IsPersistent) cookie.Expires = ticket.Expiration; 
    Response.Cookies.Add(cookie); 
+0

+1有时我绝望,我真的这样做。感觉我们正在教世界基本的编程技术,因为他们不会被打扰? :) – Liam

相关问题