Asp.net-Mvc

使用帶有 UseOAuthBearerTokens 方法的 ASP.NET Identity 2.0 UserManagerFactory 的範例?

  • May 19, 2016

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);

並且不知道如何用UserManagerFactory2.0 alpha 樣本中的此類呼叫替換上述內容,同時仍OAuthBearerTokens使用 SPA 模板中使用的對象:

       app.UseDbContextFactory(ApplicationDbContext.Create);

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

謝謝…-本

我在這裡添加存根,向您展示如何使用 OAuthBearerTokens…您不必使用您在 SPA 中使用的 UserManagerFactory。您可以將其切換為使用 PerOWINContext 模式。

啟動.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;

引用自:https://stackoverflow.com/questions/21519226