[RESOLVED]Claims Authentication : Object reference not set to an instance of an object. MVC 5

Hello,

I’m trying to implement Claims Authentication in to my site, So far I have all the code and the config settings but when I run it I get

Object reference not set to an instance of an object.

on the last line

  public bool UserLoggedIn(UserProfile userProfile)
        {
            var userCredentials = new[] {
                    new Claim("UserId", Convert.ToString(userProfile.UserId)),
                    new Claim("UniqueId", Convert.ToString(userProfile.UniqueId)),
                    new Claim("Username", Convert.ToString(userProfile.Username)),
                    new Claim("Firstname", Convert.ToString(userProfile.Firstname)),
                    new Claim("Surname", Convert.ToString(userProfile.Surname)),
                    new Claim("Email", Convert.ToString(userProfile.EmailAddress))};

            var id = new ClaimsIdentity(userCredentials, "Forms");
            var cp = new ClaimsPrincipal(id);
            var token = new SessionSecurityToken(cp);
            var sam = FederatedAuthentication.SessionAuthenticationModule;

            sam.WriteSessionTokenToCookie(token); // Here is where the error is

            return true;
        }

now im not sure what I’m missing but heres the config settings

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit

http://go.microsoft.com/fwlink/?LinkId=301880

  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=SCOTTS-LAPTOPLOCALDB;Initial Catalog=TestDatabase; Trusted_Connection=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>

  <system.webServer>
    <modules>
      <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </modules>
  </system.webServer>

  <system.identityModel.services>
    <federationConfiguration>
      <cookieHandler requireSsl="false"></cookieHandler>
    </federationConfiguration>
  </system.identityModel.services>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Please help.

Thank you

Hi,

Harrison.Scott

var sam = FederatedAuthentication.SessionAuthenticationModule;

            sam.WriteSessionTokenToCookie(token); // Here is where the error is

It seems variable "sam" is an enum constant. Note that, you can’t add or call any methods on enum constant.

Check this site, may help u:

http://msdn.microsoft.com/en-us/library/microsoft.identitymodel.web.sessionauthenticationmodule.writesessiontokentocookie.aspx

Hi Raju,

I have been through that site, and It didn’t help me unfortunately.

Any other suggestions?

Hi,

Checking documentation for "FederatedAuthentication", i found its not enum but static class.

check this site may help u:

http://msdn.microsoft.com/en-us/library/gg638729.aspx

Leave a Reply