跨域cookie身份验证问题WebApi ASP.NET

跨域cookie身份验证问题WebApi ASP.NET,asp.net,asp.net-mvc,asp.net-web-api,cors,asp.net-identity,Asp.net,Asp.net Mvc,Asp.net Web Api,Cors,Asp.net Identity,我想使用cookie授权从MVC站点向WebApi发出ajax请求。 但是 我陷入了困境 ControllerContext.RequestContext.Principal 为空。它似乎无法识别cookies,除非它存在于请求中。 我有两份申请 1-MVC是主要的 2-WebApi附加 MVC请求WebApi。两者都使用公共身份用户 这是我的实现 IAppBuilder的注册 public static void Register(IAppBuilder app) {

我想使用cookie授权从MVC站点向WebApi发出ajax请求。 但是 我陷入了困境

ControllerContext.RequestContext.Principal
为空。它似乎无法识别cookies,除非它存在于请求中。 我有两份申请 1-MVC是主要的 2-WebApi附加 MVC请求WebApi。两者都使用公共身份用户

这是我的实现

  • IAppBuilder的注册

        public static void Register(IAppBuilder app)
        {
            app.CreatePerOwinContext(MyDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                CookieName = ".AspNet.Cookies",
                CookieSecure = CookieSecureOption.Never,
                AuthenticationMode = AuthenticationMode.Active
            });
    
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        }
    

  • 请提供帮助。

    CORS规范规定,如果启用
    支持凭据
    ,则不允许将
    来源设置为
    “*”
    。见标题上方的段落

    如果您将
    原点设置为某个特定的值,它是否有效

    当客户端点击
    /token
    端点或在授权发生后点击真实端点时,请求是否也会失败


    如果它无法命中您的
    /token
    端点,那么您需要卸载
    Microsoft.AspNet.WebApi.Cors
    ,并改为安装
    Microsoft.Owin.Cors
    使用它。

    Cors规范规定,如果启用
    支持凭据
    ,则不允许将
    源代码
    设置为
    “*”
    。见标题上方的段落

    如果您将
    原点设置为某个特定的值,它是否有效

    当客户端点击
    /token
    端点或在授权发生后点击真实端点时,请求是否也会失败

    如果它无法命中您的
    /token
    端点,那么您需要卸载
    Microsoft.AspNet.WebApi.Cors
    ,并改为安装
    Microsoft.Owin.Cors

    public class ApplicationUserManager : UserManager<ApplicationUser> {
    public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store)
    {
    }
    
    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var appDbContext = context.Get<MyDbContext>();
        var appUserManager = new ApplicationUserManager(new UserStore<ApplicationUser>(appDbContext));
        return appUserManager;
    } }
    
    [Authorize]
    [EnableCors(origins: "*", headers: "*", methods: "*", SupportsCredentials = true)]
    public sealed class BalanceController : ApiController ...