Tôi có một dự án API Web sử dụng UseJwtBearerAuthentication cho máy chủ định danh của tôi. Phương thức cấu hình khi khởi động có dạng như sau:MVC-6 vs MVC-5 BearerAuthentication trong API Web
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();
}
Điều này đang hoạt động và tôi muốn làm điều tương tự trong dự án MVC5. Tôi cố gắng để làm một cái gì đó như thế này:
api web:
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;
}
}
tôi không thể gọi api web nếu là thiết lập thuộc tính [ủy quyền] (Nếu tôi loại bỏ điều này hơn là nó đang làm việc)
Tôi đã tạo Startup. Mã này không bao giờ được gọi và tôi không biết phải thay đổi gì để làm cho nó hoạt động.
[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);
}
}
}
mục tiêu là trả lại xác nhận quyền sở hữu từ api web cho ứng dụng khách. bằng cách sử dụng xác thực Bearer.
Cảm ơn bạn đã trợ giúp.
Tôi tìm ứng dụng ở đâu.UseJwtBearerAuthentication (...)? Project.json của bạn và sử dụng các tham chiếu (trên Startup.cs) trông như thế nào? –
"Microsoft.AspNet.Authentication.JwtBearer": "1.0.0- *" – Raskolnikov
Trên mã Khởi động của bạn không được gọi, bạn có thể cần một tập hợp các phụ thuộc nuget phù hợp với MVC5. Tôi chỉ sử dụng phương pháp Global.asax.cs 'Application_Start' thay vì' OwinStartup' – user326608