9

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: 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);
    }
}

Edit: new error arised:

The following errors occurred while attempting to load the app.
- No assembly found containing an OwinStartupAttribute.
- No assembly found containing a Startup or [AssemblyName].Startup class.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config.
To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.
Nkosi
  • 235,767
  • 35
  • 427
  • 472
User987
  • 3,663
  • 15
  • 54
  • 115
  • Anyone you guys ? :/ I've been trying to find out where this AuthenticationManager class is for hours, no luck so far... – User987 Oct 28 '16 at 13:34
  • [System.Net.AuthenticationManager](https://msdn.microsoft.com/en-us/library/system.net.authenticationmanager(v=vs.110).aspx) does not have a SignIn method. Are you using OWIN? check this https://msdn.microsoft.com/en-us/library/microsoft.owin.security.iauthenticationmanager.signin(v=vs.113).aspx#M:Microsoft.Owin.Security.IAuthenticationManager.SignIn(System.Security.Claims.ClaimsIdentity[]) – Nkosi Oct 28 '16 at 13:42
  • @Nkosi yes I'm using OWIN, I have it referenced in my application... But for some reason it's not able to call the SignIn method... – User987 Oct 28 '16 at 13:47
  • Looks like your example code is refering to the wrong type. You are going to have to show more code. show a [mcve] of the controller you are calling that code from – Nkosi Oct 28 '16 at 13:49
  • @Nkosi I have updated my question with all of the namespaces I'm using, alongside with the complete code for login action in the controller – User987 Oct 28 '16 at 13:52
  • Where are you using AuthenticationManager? Update doesn't show it – Nkosi Oct 28 '16 at 13:54
  • Edited again, my apologies ... – User987 Oct 28 '16 at 13:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126913/discussion-between-user987-and-nkosi). – User987 Oct 28 '16 at 13:54
  • is there an `AuthenticationManager` property on the controller? – Nkosi Oct 28 '16 at 13:55
  • Yes but it only refers to the System.Net.AuthenticationManager .... It doesn't shows the one from the OWIN library for some reason :/ – User987 Oct 28 '16 at 13:56
  • Show where you declare the property – Nkosi Oct 28 '16 at 13:57
  • I haven't declared it anywhere yet.... The documentation says I only call the static method from the class named: "SignIn" And pass it the identity parameter, which is what I've done essentially ... – User987 Oct 28 '16 at 13:59
  • It is not a static method – Nkosi Oct 28 '16 at 13:59
  • @Nkosi I have another problem now, I've referenced everything properly but I'm getting an error that I'm missing something in Startup class... I've created a fresh empty MVC project without the startup class. I've edited my question, can you look into it ? – User987 Oct 28 '16 at 14:12
  • That's a new question. and also a repeated one on SO. check SO and you will see the solution. – Nkosi Oct 28 '16 at 14:13

1 Answers1

7

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;
        }
    }    
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • This seems to be the right thing, but now I'm getting error: "HttpContextBase" does not contains a method "GetOwinContext"... – User987 Oct 28 '16 at 14:03
  • 3
    Are you sure you have Owin referenced in the project – Nkosi Oct 28 '16 at 14:03
  • 2
    Yeah you have a good point, I missed to install a few Owin packages from nuget... :) It works now, thanks !! :) – User987 Oct 28 '16 at 14:07
  • Specifically, it's contained in the `Microsoft.Owin.Host.SystemWeb` package. – w5l Jun 10 '20 at 08:28