c# - Web API 中的 MVC-6 与 MVC-5 承载身份验证

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

我有一个将 UseJwtBearerAuthentication 用于我的身份服务器的 Web API 项目。 启动时的配置方法如下所示:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseJwtBearerAuthentication(options =>
    {
        options.AutomaticAuthentication = true;
        options.Authority = "http://localhost:54540/";
        options.Audience = "http://localhost:54540/";
    });

    // Configure the HTTP request pipeline.
    app.UseStaticFiles();

    // Add MVC to the request pipeline.
    app.UseMvc();
}

这很有效,我想在 MVC5 项目中做同样的事情。我试图做这样的事情:

网络接口(interface):

public class SecuredController : ApiController
    {
            [HttpGet]
            [Authorize]
            public IEnumerable<Tuple<string, string>> Get()
            {
                var claimsList = new List<Tuple<string, string>>();
                var identity = (ClaimsIdentity)User.Identity;
                foreach (var claim in identity.Claims)
                {
                    claimsList.Add(new Tuple<string, string>(claim.Type, claim.Value));
                }
                claimsList.Add(new Tuple<string, string>("aaa", "bbb"));

                return claimsList;
            }
}

如果设置了 [授权] 属性,我将无法调用 web api(如果我删除它,它就会正常工作)

我创建了启动。此代码从未被调用,我不知道要更改什么才能使其正常工作。

[assembly: OwinStartup(typeof(ProAuth.Mvc5WebApi.Startup))]
namespace ProAuth.Mvc5WebApi
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureOAuth(app);
            HttpConfiguration config = new HttpConfiguration();
            WebApiConfig.Register(config);
            app.UseWebApi(config);
        }

        public void ConfigureOAuth(IAppBuilder app)
        {
            Uri uri= new Uri("http://localhost:54540/");
            PathString path= PathString.FromUriComponent(uri);

            OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
            {
                AllowInsecureHttp = true,
                TokenEndpointPath = path,
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                Provider = new SimpleAuthorizationServerProvider()
            };

            // Token Generation
            app.UseOAuthAuthorizationServer(OAuthServerOptions);
            app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

        }

    }

    public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
    {
        public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            context.Validated();
        }

        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            identity.AddClaim(new Claim("sub", context.UserName));
            identity.AddClaim(new Claim("role", "user"));

            context.Validated(identity);

        }
    }

}

目标是将声明从网络 API 返回到客户端应用程序。使用不记名身份验证。
感谢您的帮助。

最佳答案

TL;DR:你不能。

Authority是指在ASP.NET 5的承载中间件中加入的一个OpenID Connect特性:OWIN/Katana版本没有。

注意:Katana 有一个 app.UseJwtBearerAuthentication 扩展,但与其 ASP.NET 5 等价物不同,它不使用任何 OpenID Connect 功能,必须手动配置:您必须提供发行者名称和用于验证 token 签名的证书:https://github.com/jchannon/katanaproject/blob/master/src/Microsoft.Owin.Security.Jwt/JwtBearerAuthenticationExtensions.cs

关于c# - Web API 中的 MVC-6 与 MVC-5 承载身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32581491/

相关文章:

c# - 使用 OData 在 .NET API 上返回嵌套对象

c# - 如何解决 EM_WATCHDOG_TIMEOUT_DEADA444 SICK_APPLICATION_DEADA444 上的 Windows Phone 8.1 运行时崩溃

c# - 如何在自分层树结构中的每个节点执行业务规则

Java HttpClient 使用 session ID 访问图像

mysql - 从mysql中的加密密码生成相应的明文

c# - 从自托管的 WEB API 控制台应用程序返回 jsonp

c# - Restful 网址。查看特定型号

c# - 带接口(interface)的架构/设计(重构帮助)

c# - ProtoBuf WCF 行为的配置错误

java - tomcat 7重启后恢复用户登录