【发布时间】:2018-10-27 21:08:48
【问题描述】:
在我的 web api 应用程序中,我实现了 OAuth2。在 ApplicationOAuthProvider 的 GrantResourceOwnerCredentials 上,我正在调用我的自定义会员服务以登录并获取令牌。问题是我必须将会员服务注入 ApplicationOAuthProvider 才能使用该服务,但由于 owinStartup 类不允许它不' t 支持参数构造函数。如何在 GrantResourceOwnerCredentials 方法中注入/使用我的会员服务。
public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
{
private readonly string _publicClientId;
private readonly IMembershipService _membershipService;
public ApplicationOAuthProvider(string publicClientId,
IMembershipService membershipService)
{
if (publicClientId == null)
{
throw new ArgumentNullException("publicClientId");
}
_publicClientId = publicClientId;
this._membershipService = membershipService;
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
AccountLogin LogCredentials = new AccountLogin();
LogCredentials.UserName = context.UserName;
LogCredentials.Password = context.Password;
ProviderLoginResponse providerLoginResponse =
_membershipService.UserLogin(LogCredentials);
if (providerLoginResponse.LoginStatus != "Y")
{
context.SetError("invalid_grant", "The user name or password
is incorrect.");
return;
}
var claims = new List<Claim>()
{
new Claim(ClaimTypes.Sid, Convert.ToString(1)),
new Claim(ClaimTypes.Name, providerLoginResponse.UserName),
new Claim(ClaimTypes.Email, providerLoginResponse.UserEmail)
};
ClaimsIdentity oAuthIdentity = new ClaimsIdentity(claims,
Startup.OAuthOptions.AuthenticationType);
AuthenticationProperties properties = CreateProperties(context.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
}
}
我自己的创业班:
public partial class Startup
{
private readonly IMembershipService _membershipService;
//This will cause a runtime error owin startup class only support parameterless constructor
public Startup(IMembershipService membershipService)
{
this._membershipService = membershipService;
}
public void ConfigureAuth(IAppBuilder app)
{
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
//Here passing the _membershipService to ApplicationOAuthProvider constructor
Provider = new ApplicationOAuthProvider(PublicClientId,_membershipService ),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
// In production mode set AllowInsecureHttp = false
AllowInsecureHttp = true
};
}
}
【问题讨论】:
-
@Marcus Höglund 我在我的项目中使用 autofac 我把 public static IContainer IoC { get;放; } 在启动类。 var builder = new ContainerBuilder(); builder.RegisterType
() .As ().InstancePerLifetimeScope(); var container = builder.Build(); Startup.IoC = 容器;但是 IContainer 没有任何解析方法 – Mussammil -
您可以将命名空间“Autofac.ResolutionExtensions”添加到具有 Resolve
() 方法的启动类中。 -
@Marcus Höglund Autofac.ResolutionExtensions.Resolve 具有签名 public static TService Resolve
(this IComponentContext context) ,所以我从哪里得到 IComponentContext。我无法在启动类中注入 IComponentContext 作为好 -
在这里看看这个答案。在 ContainerBuilder 上调用 build 后,它会返回一个 IContainer。将其存储在 Startup.cs 中,然后在 stackoverflow.com/questions/6262704/… 上调用 Resolve
标签: asp.net-mvc asp.net-web-api dependency-injection oauth-2.0 owin