2016-10-28 7 views
3

私はAuthenticationManagerクラスSignIn()のメソッドを使用しようとしています。 AuthenticationManagerへのパスがあるAuthenticationManager.SignIn()がAuthenticationManagerクラスに存在しません

AuthenticationManager.SignIn(identity); 

しかし、それはサインインが存在しないと言う...

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

編集:新しいエラーが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. 
+0

誰でも君たちを使用してここで、コントローラのミニバージョンです? :/ /私は、このAuthenticationManagerクラスが何時間何時になっているかを知ろうとしていますが、これまでのところ運が無かった... – User987

+0

[System.Net.AuthenticationManager](https://msdn.microsoft.com/en-us/library/) system.net.authenticationmanager(v = vs.110).aspx)には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

+0

@Nkosiはい私はOWINを使用していますが、私のアプリケーションで参照しています...しかし何らかの理由でSignInメソッドを呼び出せません... – User987

答えて

3

簡体例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; 
     } 
    } 

} 
+0

これは正しいことですが、エラーが出ます: "HttpContextBase"には "GetOwinContext"メソッドが含まれていません... – User987

+1

Owinがプロジェクトで参照されていますか? – Nkosi

+1

あなたは良い点を持っています。私はNugetからいくつかのOwinパッケージをインストールするのを見逃していました... :)それは今、感謝の仕事! :) – User987