2个具有Web Api身份验证的MVC应用程序

时间:2017-01-13 14:24:42

标签: c# angularjs asp.net-mvc asp.net-web-api owin

我有2个不同的应用程序都使用MVC和使用AngularJS调用的web api。

访问应用程序#1以检查并查看他们是否可以访问应用程序#2。如果他们这样做我试图从#1调用应用程序#2的Web API,这可以正常工作。

问题是如何通过OWIN进行身份验证?我已经知道它已经登录了应用程序#2的Web API上的所有内容,但是当角度调用回来时,并尝试从#1导航 - > #2,我不再被认证了。

API#2上的控制器。

//Setup Identity above
var ctx = Request.GetOwinContext();
    var authManager = ctx.Authentication;
    authManager.SignIn(identity);

MVC / API#2上的OWIN设置

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "ApplicationCookie",
            LoginPath = new PathString("/Account/Login"),
            SlidingExpiration = true,
            ExpireTimeSpan = new TimeSpan(1, 0, 0),
            CookieHttpOnly = true,
            Provider = new CookieAuthenticationProvider
            {
                OnApplyRedirect = ctx =>
                {
                    if (!ctx.Request.Path.StartsWithSegments(new PathString("/api")))
                    {
                        ctx.Response.Redirect(ctx.RedirectUri);
                    }
                    else
                    {
                        ctx.Response.StatusCode = 401;
                    }
                },

            }
        });

这是可行的还是不起作用,因为我从其他域调用它并且我无法检索应用程序cookie?

0 个答案:

没有答案
相关问题