相关疑难解决方法(0)

Web API中的MVC-6与MVC-5 BearerAuthentication

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

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();
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,我想在MVC5项目中做同样的事情.我试着这样做:

Web api:

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 …
Run Code Online (Sandbox Code Playgroud)

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

14
推荐指数
1
解决办法
4377
查看次数