gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:58:11 28 4
gpt4 key购买 nike

我有一个将 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com