Owin Authenticate не хранится - PullRequest
0 голосов
/ 15 октября 2019

Я использую аутентификацию Owin в своем проекте mvc, но не могу понять, почему значения метода SignIN теряются после того, как я возвращаю Redirect (Url.Action ("Index", "Home"));Так что попадаем в бесконечный цикл входа в систему

И я последовал этому уроку: https://medium.com/@wiliambuzatto/asp-net-indentity-limpo-e-despido-mvc-e9e8f73c0d25

AuthController

 [AllowAnonymous]
public class AuthController : Controller
{
    [HttpGet]
    public ActionResult Login()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login(LoginModel login)
    {
        if (!ModelState.IsValid)
        {
            return View();
        }

        var result = new LoginPageViewModel().Login(login);

        var identity = new ClaimsIdentity(new[] {
            new Claim(ClaimTypes.Name, result.Nome),
          //  new Claim("BearerToken", result.BearerToken)
        }, "AplicationToken");

        var ctx = Request.GetOwinContext();
        var authManager = ctx.Authentication;

        authManager.SignIn(identity);

        return Redirect(Url.Action("Index", "Home"));
    }
}

Startup.cs

using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Owin;

[assembly: OwinStartup(typeof(AvaUnirpMVC.Startup.Startup))]

namespace AvaUnirpMVC.Startup
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
        public void ConfigureAuth(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "AplicationCookie",
                LoginPath = new PathString("/auth/login")
            });
        }
    }
}

RouteConfig

 public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

@ Редактировать (решено) Я просто изменил метод ConfigureAuth () на:

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/auth/login")
        });
...