AuthenticationManager类中不存在AuthenticationManager.SignIn()

Use*_*987 7 c# asp.net authentication asp.net-mvc

我正在尝试使用AuthenticationManager该类中的方法SignIn();

我是这样做的:

AuthenticationManager.SignIn(identity);
Run Code Online (Sandbox Code Playgroud)

但它说那里SignIn不存在......

通往的路径AuthenticationManager是:

System.Net.AuthenticationManager
Run Code Online (Sandbox Code Playgroud)

我在这里遗漏了什么吗???

编辑:这是控制器的迷你版本:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:出现新错误:

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.
Run Code Online (Sandbox Code Playgroud)

Nko*_*osi 6

控制器使用的简化示例 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;
        }
    }    
}
Run Code Online (Sandbox Code Playgroud)

  • 你确定你在项目中引用了Owin吗? (3认同)
  • 是的,你有一个好点,我错过了从nuget安装一些Owin包... :)它现在有效,谢谢!:) (2认同)