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

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 使用ASP.NET Identity 2.0 UserManagerFactory与UseOAuthBearerTokens方法的示例?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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
            }
        });

谢谢…
-ben

解决方法

我在这里添加存根,显示如何使用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),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相关文章