User987 User987 - 1 month ago 8
ASP.NET (C#) Question

AuthenticationManager.SignIn() isn't present in AuthenticationManager class

I'm trying to use the method from the AuthenticationManager class SignIn();

Here is how I'm doing it:

AuthenticationManager.SignIn(identity);


But it says that SignIn doesn't exists there...

The path to the AuthenticationManager is:

System.Net.AuthenticationManager


Am I missing something here???

Edit: @Nkosi here is the mini version of the controller:

using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using System;
using System.Linq;
using System.Security.Claims;
using System.Web.Mvc;
using WebApplication2.Models;
using WebApplication2.ViewModels;

[HttpPost]
[ActionName("Login")]
public ActionResult Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
string userName = (string)Session["UserName"];
string[] userRoles = (string[])Session["UserRoles"];

ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);

identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userName));

userRoles.ToList().ForEach((role) => identity.AddClaim(new Claim(ClaimTypes.Role, role)));

identity.AddClaim(new Claim(ClaimTypes.Name, userName));

AuthenticationManager.SignIn(identity);
return RedirectToAction("Success");
}
else
{
return View("Login",model);
}
}


Does this helps ?

Answer

Simplified example of controller using IAuthenticationManager

using Microsoft.Owin.Security;
using System.Web;    
//...other usings

public class AccountController : Controller {

    [HttpPost]
    [ActionName("Login")]
    public ActionResult Login(LoginViewModel model)
    {
        if (ModelState.IsValid)
        {
            string userName = (string)Session["UserName"];
            string[] userRoles = (string[])Session["UserRoles"];

            ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);

            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userName));

            userRoles.ToList().ForEach((role) => identity.AddClaim(new Claim(ClaimTypes.Role, role)));

            identity.AddClaim(new Claim(ClaimTypes.Name, userName));

            AuthenticationManager.SignIn(identity);
            return RedirectToAction("Success");
        }
        else
        {
            return View("Login",model);
        }
    }

    private IAuthenticationManager AuthenticationManager {
        get {
            return HttpContext.GetOwinContext().Authentication;
        }
    }

}