Owin身份验证未存储

时间:2019-10-15 14:38:57

标签: c# asp.net-mvc authentication owin

我在我的mvc项目中使用Owin身份验证,但是我找不到为什么在返回Redirect之后,SignIN方法的值会丢失(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 }
        );
    }
}

@Edit(已解决) 我只是将方法ConfigureAuth()更改为:

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

0 个答案:

没有答案