AuthenticationManager类中不存在AuthenticationManager.SignIn()方法。

9

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

我的做法如下:

AuthenticationManager.SignIn(identity);

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

AuthenticationManager 的路径是:

System.Net.AuthenticationManager

我有点不明白这里是什么意思???

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

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

编辑:新的错误出现了。
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.

有人在吗? :/ 我已经试了好几个小时,还是找不到这个 AuthenticationManager 类在哪里... - User987
System.Net.AuthenticationManager 没有 SignIn 方法。您是否在使用 OWIN?请查看此链接 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
让我们在聊天中继续这个讨论 - User987
是的,但它只涉及到System.Net.AuthenticationManager……出于某种原因,它没有显示OWIN库中的那个:/ - User987
这不是一个静态方法。 - Nkosi
显示剩余10条评论
1个回答

7

使用 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;
        }
    }    
}

这似乎是正确的事情,但现在我遇到了错误:“HttpContextBase”不包含方法“GetOwinContext”... - User987
3
你确定项目中已经引用了Owin吗? - Nkosi
2
是的,你说得对,我忘记从NuGet安装一些Owin包了... :) 现在它可以工作了,谢谢!! :) - User987
具体来说,它包含在 Microsoft.Owin.Host.SystemWeb 包中。 - w5l

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接