MVC 项目中的 Web API(身份验证)
Web API in MVC project (Authentication)
我有 ASP.NET MVC 项目,并使用具有 ASP.NET 身份的表单身份验证(基于 Cookie)。我在该项目中添加了 WEB API 控制器。现在我想要的是对于 MVC 项目,它应该使用使用 Cookie 的表单身份验证,但是对于 API 它应该使用令牌库,我如何配置它。
此致,
伊姆兰艾哈迈德
首先安装这些 NuGet 包,
1.Install-Package Microsoft.AspNet.WebApi.Owin
2.Install-Package Microsoft.Owin.Host.SystemWeb
3.Install-Package Microsoft.Owin.Security.OAuth
那么,项目必须有一个 StartUp.cs 文件。
将此代码添加到您的文件
OAuthAuthorizationServerOptions OAuthOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(2),
Provider = new AuthorizationServerProvider()
};
// To Generate token
app.UseOAuthAuthorizationServer(OAuthOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
将此代码添加到新的 AuthorizationServerProvider.cs 文件
public class AuthorizationServerProvider: OAuthAuthorizationServerProvider
{
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
context.Validated(identity);
}
}
您可以使用 Postman API 客户端测试代码,方法是发布到您的端点
yourwebsite/api/token
连同
grant_type
,用户名和密码在
x-www-form-Urlencoded
邮递员的标签。
您将获得 access_token 的响应,将此访问令牌放入您的 header 中,同时调用您的 Resource
控制器。
如需进一步参考,请参阅 http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/。
我有 ASP.NET MVC 项目,并使用具有 ASP.NET 身份的表单身份验证(基于 Cookie)。我在该项目中添加了 WEB API 控制器。现在我想要的是对于 MVC 项目,它应该使用使用 Cookie 的表单身份验证,但是对于 API 它应该使用令牌库,我如何配置它。
此致, 伊姆兰艾哈迈德
首先安装这些 NuGet 包,
1.Install-Package Microsoft.AspNet.WebApi.Owin
2.Install-Package Microsoft.Owin.Host.SystemWeb
3.Install-Package Microsoft.Owin.Security.OAuth
那么,项目必须有一个 StartUp.cs 文件。
将此代码添加到您的文件
OAuthAuthorizationServerOptions OAuthOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(2),
Provider = new AuthorizationServerProvider()
};
// To Generate token
app.UseOAuthAuthorizationServer(OAuthOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
将此代码添加到新的 AuthorizationServerProvider.cs 文件
public class AuthorizationServerProvider: OAuthAuthorizationServerProvider
{
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
context.Validated(identity);
}
}
您可以使用 Postman API 客户端测试代码,方法是发布到您的端点
yourwebsite/api/token
连同
grant_type
,用户名和密码在
x-www-form-Urlencoded
邮递员的标签。
您将获得 access_token 的响应,将此访问令牌放入您的 header 中,同时调用您的 Resource
控制器。
如需进一步参考,请参阅 http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/。