2016-06-08 48 views
2

Ich versuche, einen Login-Bildschirm mit owin auf Asp MVC zu erstellen. Dies ist der Code, der in der Steuerung ist.Benutzerdefinierte Auth mit OWIN auf Asp Mvc

Ich habe sogar versucht, hart die Codierung der Werte, aber ich bekomme immer noch eine 401, wenn ich versuche, das Dashboard nach dem Login eingeben.

[HttpPost] 
    public ActionResult Login(string username, string password) 
    { 
     int userId = 0; 
     string role = string.Empty; 

     if (new UserManager().IsValid(username, password, ref userId, ref role)) 
     { 
      var ident = new ClaimsIdentity(
       new[] { 
      // adding following 2 claim just for supporting default antiforgery provider 
      new Claim(ClaimTypes.NameIdentifier, userId.ToString()), 
      new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"), 

      new Claim(ClaimTypes.Name,username), 

      // optionally you could add roles if any 
      new Claim(ClaimTypes.Role, role) 

       }, 
       DefaultAuthenticationTypes.ApplicationCookie); 

      HttpContext.GetOwinContext().Authentication.SignIn(
       new AuthenticationProperties { IsPersistent = false }, ident); 
      return RedirectToAction("Dashboard"); // auth succeed 
     } 
     // invalid username or password 
     ModelState.AddModelError("", "invalid username or password"); 
     return View("Index", "_LayoutUnAuthorised"); 
    } 

    [Authorize(Roles = "Default")] 
    public ActionResult Dashboard() 
    { 



     return View(); 
    } 

Meine Startdatei ist leer bin ich etwas fehlt hier

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 

    } 
} 

Antwort

3

Ja, verpassten Sie die Owin Cookie-Konfiguration auf Startklasse:

public void Configuration(IAppBuilder app) 
{ 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationMode = AuthenticationMode.Active, 
     AuthenticationType = "ApplicationCookie", 
     LoginPath = new PathString("/Account/LogOn"), 
    }); 
} 

installieren Nuget Package Microsoft.Owin.Security.Cookies dann sind Sie gut zu gehen