为什么 asp.net 5.0 预览版没有身份验证 - web api 模板 -

Why is there no authentication for the asp.net 5.0 preview - web api template -

我在新 Visual Studio 2015 年创建了一个 Web 项目。

我可以选择 asp.net 4.6 或 5.0 预览网页 api 模板。旧的 4.6 有

身份验证,但我也想使用新的 5.0 预览版 api。

但是这个模板没有认证,为什么?

在 OWIN 世界中,您可以在需要时提供所需的身份验证。他是 ASP.NET 5 世界的新范式。 “你只会得到你明确表示需要的东西。如果你不要求,你就得不到”。这只是这种心态的另一个例子。

Scott Guthrie 在他最近的 post 中提到了这一点:

ASP.NET 5 introduces a new HTTP request pipeline that is modular so you can add only the components that you need. The pipeline is also no longer dependent on System.Web. By reducing the overhead in the pipeline, your app can experience better throughput and a more tuned HTTP stack. The new pipeline is based on many of the learnings from the Katana project and also supports OWIN.

To customize which components are used in the pipeline, use the Configure method in your Startup class. The Configure method is used to specify which middleware you want to “use” in your request pipeline. ASP.NET 5 already includes ported versions of many of the middleware from the Katana project, like middleware for static files, authentication and diagnostics. The following image shows some of the features you can add or remove to the pipeline for your project.

您可以非常快速地插入您的安全;你只需要指定你将使用的是什么。

public void Configure(IApplicationBuilder app)
{
    // Add static files to the request pipeline.
    app.UseStaticFiles();
 
    // Add cookie-based authentication to the request pipeline.
    app.UseIdentity();
 
    // Add MVC and routing to the request pipeline.
    app.UseMvc(routes =>
    {
    routes.MapRoute(
        name: "default",
        template: "{controller}/{action}/{id?}",
        defaults: new { controller = "Home", action = "Index" });
 
});

他们确实故意遗漏了它,因为它不再是 ASP.NET 的构建方式。这是一个完全重写,不再依赖于 System.Web,这将是以前的安全模板工作所必需的。该框架采用了他们从 OWIN 中学到的知识并加以整合。 ASP.NET 5框架是OWIN的新品种。但是中间件的添加完全一样。以前的 OWIN 中间件库应该可以在 ASP.NET 5.

中使用

@Pascal @DavidL 的意思是,在您的 Web 应用程序的管道中,您将身份验证添加为中间件。您可以将此作为 IAppBuilder 上的扩展方法。

首先,您需要在 project.json 文件中添加身份验证库作为依赖项:

"dependencies": {
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta4",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
    "Microsoft.AspNet.Mvc": "6.0.0-beta4",
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-*",

那你还是看看官方的例子吧MicroSoft ASP.NET Security GitHub page

看行:

 using Microsoft.AspNet.Authentication.Cookies;

和:

        app.UseCookieAuthentication(options =>
        {
            options.AutomaticAuthentication = true;
        });

        app.Run(async context =>
        {
            if (string.IsNullOrEmpty(context.User.Identity.Name))
            {
                var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "bob") }));
                       context.Authentication.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, user);
                context.Response.ContentType = "text/plain";
                await context.Response.WriteAsync("Hello First timer");
                return;
            }

            context.Response.ContentType = "text/plain";
            await context.Response.WriteAsync("Hello old timer");
        });

这应该在 MVC 6 网络应用程序中出现在 app.UseMvc() 之前。 我希望这会有所帮助:)

当您 select 在 asp.net 5 中使用 mvc 项目模板时,它仍然是一个选项。所以只需使用 mvc 模板并在其中添加您的 web api 控制器。无论如何,Web api 和 mvc 在 v5 中使用相同的控制器。当然,您可能希望将某些身份验证控制器修改为 return json 而不是视图,以更适合网络 api.

MS 为准备 asp.net5 对身份系统做了很多更改,所以他们将其从模板中排除对我来说没有意义(至少作为一个选项 - 这一直都是这样)。我预计他们还没有开始更新模板。

来自 github 自述文件:

https://github.com/aspnet/Identity

ASP.NET Identity is the new membership system for building ASP.NET web applications. ASP.NET Identity allows you to add login features to your application and makes it easy to customize data about the logged in user.

This project is part of ASP.NET vNext. You can find samples, documentation and getting started instructions for ASP.NET vNext at the Home repo.