Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a custom claims provider, which uses some services from another assembly. This service assembly basically uses Linq-to-SQL context to read data from database which then custom claims provider uses to augment claims.

The problem is, that this mechanism works OK, and suddenly users can't log in, because SecurityTokenService fails to issue token, because there is an error in custom claims provider, which comes from service assembly. It fails randomly, and I assume problem is with linq data context (see stacktrace).

For now we fix this by restarting iis periodically, but we need to find out better solution.

Example stacktrace with error:

System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.String'.
at System.Data.SqlClient.SqlBuffer.get_String()
at Read_Szkola(ObjectMaterializer`1 )
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Data.Linq.EntityRef`1.get_Entity()
at MiscServices.DAO.RocznikoKlasa.get_Szkola1()
at MiscServices.Model.ClassItem.CreateFromYearClassEntity(RocznikoKlasa klasa)

Strangest thing is that it all works and sudenly this invalid cast exception occurs. Any ideas what might be the cause and how I could solve this issue?

Update

As requested, I paste below my model:

public class ClassItem
    {
        public long ID { get; set; }
        public string Name { get; set; }
        public SchoolItem School { get; set; }
        public int StartYear { get; set; }
        public string Symbol { get; set; }

        public static ClassItem CreateFromYearClassEntity(RocznikoKlasa klasa)
        {
            if (klasa == null) throw new ArgumentNullException("klasa");
            return new ClassItem
            {
                ID = klasa.ID,
                Name = klasa.Nazwa,
                StartYear = klasa.RokRozpoczecia,
                Symbol = klasa.SymbolOddzialu,
                School = SchoolItem.CreateFromEntity(klasa.Szkola1)
            };
        }       

    }

public class ClassItem
    {
        public long ID { get; set; }
        public string Name { get; set; }
        public SchoolItem School { get; set; }
        public int StartYear { get; set; }
        public string Symbol { get; set; }

        public static ClassItem CreateFromYearClassEntity(RocznikoKlasa klasa)
        {
            if (klasa == null) throw new ArgumentNullException("klasa");
            return new ClassItem
            {
                ID = klasa.ID,
                Name = klasa.Nazwa,
                StartYear = klasa.RokRozpoczecia,
                Symbol = klasa.SymbolOddzialu,
                School = SchoolItem.CreateFromEntity(klasa.Szkola1)
            };
        }
    }

public class SchoolItem
    {
        public string Code;
        public JstItem JST;
        public string Name;
        public string City;
        public string PostCode;
        public string Street;
        public string Telephone;
        public string Fax;
        public string Email;

        public static SchoolItem CreateFromEntity(MiscServices.DAO.Szkola school)
        {
            if (school == null) throw new ArgumentNullException("school");
            return new SchoolItem {
                Code = school.Kod,
                Name = school.Nazwa,
                City = school.Miasto,
                Street = school.Ulica,
                PostCode = school.KodPocztowy,
                Telephone = school.Telefon,
                Fax = school.Fax,
                Email = school.Email,
                JST = JstItem.CreateFromEntity(school.JST1)
            };
        }
    }
share|improve this question
    
Do you mind posting the code inside CreateFromYearClassEntity(RocznikoKlasa klasa)? You might have a nullable property that is returning null or it has changed in the database and not updated in the mapping. –  F.Aquino Jan 28 '13 at 16:46
    
Looks like an datatype inference error. You may be able to get away with catching an int64 being returned and fliping it to a string before consuming it. –  ghangas Jan 28 '13 at 20:22
    
I provided implementation for my model. –  jjczopek Feb 5 '13 at 13:46

1 Answer 1

Make sure the timeout is set correctly on the security token service. This can be set in powershell as follows.

$sts = Get-SPSecurityTokenServiceConfig
$sts.FormsTokenLifeTime = (New-TimeSpan -minutes 30)
$sts.Update()
Get-SPSecurityTokenServiceConfig

Some further examples can be found here:

http://msdn.microsoft.com/en-us/library/hh147183(v=office.14).aspx http://aleksaron.wordpress.com/2012/08/09/set-token-lifetime-that-comes-from-sts/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.