通过ASP.NET 5(vnext)MVC6 beta5(无ASP.NET标识)使用OAuth身份验证

通过ASP.NET 5(vnext)MVC6 beta5(无ASP.NET标识)使用OAuth身份验证,asp.net,authentication,cookies,oauth-2.0,adfs,Asp.net,Authentication,Cookies,Oauth 2.0,Adfs,我正在尝试在ASP.NET 5 MVC6 beta5中使用OAuth身份验证,而不使用ASP.NET标识 现在,我已经设置了一个简单的cookie身份验证,如果登录表单中指定的登录名存在于数据库中,无论输入了什么密码(我不在数据库中存储用户密码),就可以登录用户 以下是我使用的软件包: "dependencies": { "Microsoft.AspNet.Authentication": "1.0.0-beta5", "Microsoft.AspNet.Authenticat

我正在尝试在ASP.NET 5 MVC6 beta5中使用OAuth身份验证,而不使用ASP.NET标识

现在,我已经设置了一个简单的cookie身份验证,如果登录表单中指定的登录名存在于数据库中,无论输入了什么密码(我不在数据库中存储用户密码),就可以登录用户

以下是我使用的软件包:

"dependencies": {

    "Microsoft.AspNet.Authentication": "1.0.0-beta5",
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-beta5",
    "Microsoft.AspNet.Authentication.OAuth": "1.0.0-beta5",     
    "Microsoft.AspNet.Mvc": "6.0.0-beta5",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta5",
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta5",
    "Microsoft.AspNet.Http": "1.0.0-beta5",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta5",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta5",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta5",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-beta5",
    "Microsoft.Framework.Configuration.UserSecrets": "1.0.0-beta5",
    "Microsoft.Framework.Logging": "1.0.0-beta5",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta5",
    "Microsoft.Net.Http.Server": "1.0.0-beta5",
    "EntityFramework": "6.1.3",
},
我在Startup.cs Configure方法中设置了cookie身份验证(就在app.UseMvc之前):

以下是我在帐户控制器中的登录方法:

//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public IActionResult Login(LoginViewModel model, string returnUrl = null)
{
    ViewBag.ReturnUrl = returnUrl;
        if (ModelState.IsValid)
        {
            User user = securityService.FindUserByUserName(model.UserName);
            if (user != null)
            {
                var claims = new List<Claim> { new Claim(ClaimTypes.Name, user.UserName) };
                var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                Context.Authentication.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

                return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
            }
        }

    return View(model);
}
//
// POST: /Account/LogOff
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult LogOff()
{
    Context.Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationScheme);
    return RedirectToAction("Login");
}
现在,我想使用OAuth身份验证中间件(如下所示)根据AD FS对用户进行身份验证:

但我不知道如何在登录操作中指定使用该中间件进行身份验证(同时传递登录视图表单中输入的登录名和密码)


有什么想法吗?

如果您想显示身份验证类型并基于源代码或其他因素进行隔离,我会深入研究MVC6过滤器,即 特别是授权。如果你想做混合动力车(Cookie如果是外部用户,oauth通过ADFS如果是内部用户,您可以让登录帖子返回一个不同的路径:即/api/登录vs/基于输入登录。因此,如果您正在尝试一个带有复选框、选项或某些元素的表单来指定ADFS路径vs Cookie路径,您可以收集您的判别标准,并在登录帖子上指示操作。)根据他们的选择选择。您可以决定更喜欢客户端驱动还是服务器驱动技术。无论哪种方式,代码都可以使用该路由或post data附录来引导路由,在
启动中,您可以执行以下操作:

public class Startup
{
    public void Configure(IApplicationBuilder app, ILoggerFactory log, IHostingEnvironment env)
    {
        app.Map(new PathString("oauth"), Action<IApplicationBuilder> x =>
        {  
            x.UseOAuthAuthentication(); ... 
        });

        app.Map(new PathString("cookie"), Action<IApplicationBuilder> x => {
 x.UseCookieAuthentication(); ....

    });
}

public class Startup
{
    public void Configure(IApplicationBuilder app, ILoggerFactory log, IHostingEnvironment env)
    {
        app.Map(new PathString("oauth"), Action<IApplicationBuilder> x =>
        {  
            x.UseOAuthAuthentication(); ... 
        });

        app.Map(new PathString("cookie"), Action<IApplicationBuilder> x => {
 x.UseCookieAuthentication(); ....

    });
Configure(IApplicationBuilder app){

    app.USeCookieAuthentication();