首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Web中的MVC-6与MVC-5 BearerAuthentication

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

Stack Overflow用户
提问于 2015-09-15 08:40:17
回答 2查看 4.9K关注 0票数 14

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

代码语言:javascript
运行
复制
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项目中做同样的事情。我试过这样做:

代码语言:javascript
运行
复制
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;
            }
}

如果设置为authorized属性,我无法调用web

我创建了创业公司。这段代码从来没有被调用过,我不知道要修改什么才能使它正常工作。

代码语言:javascript
运行
复制
[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);

        }
    }

}

目标是将声明从web返回到客户端应用程序。使用比勒身份验证。

谢谢你帮忙。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-16 00:37:43

TL博士:你不能。

Authority是指在ASP.NET 5中添加到承载中间件中的OpenID连接特性:在OWIN/Katana版本中没有这样的功能。

注意: Katana有一个app.UseJwtBearerAuthentication扩展,但与其ASP.NET 5不同,它不使用任何OpenID连接特性,必须手动配置:您必须提供颁发者名称和验证令牌签名的证书:https://github.com/jchannon/katanaproject/blob/master/src/Microsoft.Owin.Security.Jwt/JwtBearerAuthenticationExtensions.cs

票数 7
EN

Stack Overflow用户

发布于 2015-10-14 11:33:15

您可以从以下几个方面获得索赔:

代码语言:javascript
运行
复制
 IAuthenticationManager AuthenticationManager
 {
    get
    {
        return Request.GetOwinContext().Authentication;
    }
}

public IHttpActionResult UserRoles()
{
 return ok(AuthenticationManager.User.Claims.ToList());
}

此代码应该在授权控制器中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32581491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档