1

I'm trying to implement claims authentication in to my site I have followed the tutorial shown here

Replacing Forms Authentication now from what I can see I have done everything correctly... so I thought, when I run my application enter my credentials I then try to set the Claim as shown here

[HttpPost]
    public ActionResult Register(UserRegister userRegister)
    {
        if (ModelState.IsValid)
        {
            var userProfile = new RegisterService().RegisterUser(userRegister);

            if (userProfile != null)
            {
                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);

                return RedirectToAction("UserProfile", "Profile", new { area = "Profile" });
            }
            else
            {
                TempData["error"] = "Something went wrong with your registration, plese check the details you entered and try again";
            }
        }

        return View(userRegister);
    }

But the Error I get is Object reference not set to an instance of an object. on the following line of the above

sam.WriteSessionTokenToCookie(token);

I'm not sure what exactly I'm doing wrong here, reason behind this is because I have another project again with claims authentication implemented successfully.

Any help would be beneficial.

Thanks

Code Ratchet
  • 5,141
  • 11
  • 67
  • 123
  • Most likely, `sam` is null - so you **cannot** call methods on it. So you really need to figure out *why* `sam` ends up being `null` - and you need to **check** for `null` before accessing it! – marc_s Oct 28 '14 at 07:55
  • @marc_s what i don't understand is I have another project set up the exact same way, same modifications to the config etc yet that one works and this one doesn't... – Code Ratchet Oct 28 '14 at 07:58
  • I hear you - but from experience, I know - there **must be** some difference in the two projects. Something you might be overlooking right now. – marc_s Oct 28 '14 at 07:59
  • @marc_s you right there's bound to be something different and judging by the look of it at the moment its going to be something small – Code Ratchet Oct 28 '14 at 08:01
  • 1
    @marc_s bam found the issue There is no SessionAuthenticationModule added to the ASP.NET Module Pipeline. A SessionAuthenticationModule must be added if WSFederationAuthenticationModule is in the Pipeline."} not sure where and what – Code Ratchet Oct 28 '14 at 08:08

0 Answers0