gpt4 book ai didi

asp.net-mvc - 将 ASP.NET Identity 2.0 UserManagerFactory 与 UseOAuthBearerTokens 方法一起使用的示例?

转载 作者:行者123 更新时间:2023-12-02 00:04:33 24 4
gpt4 key购买 nike

ASP.NET Identity 2.0 alpha 附带了新的中间件来管理获取 UserManager 的实例(app.UseUserManagerFactory 来设置它)并获取DbContext(app.UseDbContextFactory 进行设置)。有一个示例展示了如何在 MVC 应用程序中实现此功能,但与示例不同,没有关于如何从使用 OAuthBearerTokens 的 SPA 模板中实现此功能的文档。

我目前陷入困境:

UserManagerFactory = () => new DerivedUserManager(new CustomUserStore(new CustomDbContext()));

OAuthOptions = new Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new MyApp.Web.Api.Providers.ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
app.UseOAuthBearerTokens(OAuthOptions);

并且不知道如何用 2.0 alpha 示例中的调用替换上面的 UserManagerFactory,同时仍然使用 SPA 模板中使用的 OAuthBearerTokens 对象:

        app.UseDbContextFactory(ApplicationDbContext.Create);

// Configure the UserManager
app.UseUserManagerFactory(new IdentityFactoryOptions<ApplicationUserManager>()
{
DataProtectionProvider = app.GetDataProtectionProvider(),
Provider = new IdentityFactoryProvider<ApplicationUserManager>()
{
OnCreate = ApplicationUserManager.Create
}
});

谢谢...-本

最佳答案

我在这里添加 stub ,向您展示如何使用 OAuthBearerTokens...您不必使用在 SPA 中使用的 UserManagerFactory。您可以将其切换为使用 PerOWINContext 模式。

Startup.Auth.cs

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};

ApplicationOAuthProvider.cs

public ApplicationOAuthProvider(string publicClientId)
{
if (publicClientId == null)
{
throw new ArgumentNullException("publicClientId");
}
_publicClientId = publicClientId;
}

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();

ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);

if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}

ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
DefaultAuthenticationTypes.ApplicationCookie);

AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}

 

// namespace below needed to enable GetUserManager extension of the OwinContext
using Microsoft.AspNet.Identity.Owin;

关于asp.net-mvc - 将 ASP.NET Identity 2.0 UserManagerFactory 与 UseOAuthBearerTokens 方法一起使用的示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21519226/

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