如何使用Web API验证前端(knockout.js应用程序)(ASP.NET 5 MVC6)

时间:2015-08-06 21:38:28

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

我有两个网络应用程序(前端有knockout.js,后端有web api,使用asp.net 5和mvc6)。我已经在web api上实现了身份验证,只要我在本地浏览它就可以正常工作。使用microsoft帐户进行本地密码登录和外部登录都可以正常工作。

现在,我想从knockout.js前端访问web api,我不确定在ajax调用(基于密码的身份验证)和外部身份验证之后返回到前端的内容。< / p>

Startup.Configure的代码是

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.MinimumLevel = LogLevel.Information;
        loggerFactory.AddConsole();

        app.UseCors(policy=>policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
        app.UseCookieAuthentication(options =>
        {
            options.LoginPath = "/account/login";
            options.AuthenticationScheme = "Cookies";
            options.AutomaticAuthentication = true;

        });
        app.UseMicrosoftAccountAuthentication(options =>
        {
            options.ClientId = configuration["Auth:Microsoft:ClientId"];
            options.ClientSecret = configuration["Auth:Microsoft:ClientSecret"];
            options.AuthenticationScheme = "Microsoft";
            options.SignInScheme = "Cookies";
        });
        app.UseIdentity();

        app.UseMvcWithDefaultRoute();
    }

帐户控制器中的内容是

    [HttpPost]
    [AllowAnonymous]
    public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
    {
        if (ModelState.IsValid)
        {
            var result = await signInManager.PasswordSignInAsync(model.email, model.password, model.rememberMe, false);
            if (result.Succeeded)
                return Ok();


            var parsedResult = new FailedSignInResult
            {
                isLockedOut = result.IsLockedOut, isNotAllowed = result.IsNotAllowed, requireTwoFactor = result.RequiresTwoFactor
            };
            return FailWith(parsedResult,"Error authenticating account");
        }
        return FailWith(ModelState, "Model invalid");
    }

    [AllowAnonymous]
    public IActionResult External(string provider)
    {
        var props = new AuthenticationProperties
        {
            RedirectUri = "/signin-microsoft"
        };
        return new ChallengeResult(provider, props);
    }

我已经看了很多关于这个的帖子,并且有关于令牌的一些谈话,但不确定它是如何适合这里的。其他一些帖子似乎回答了以前版本在ASP.NET上的问题,但没有回答Asp.net 5.此次相关的任何链接都将受到赞赏。

0 个答案:

没有答案