加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 使用ASP.NET Identity 2.0 UserManagerFactory与

发布时间:2020-12-15 22:56:59 所属栏目:asp.Net 来源:网络整理
导读:ASP.NET Identity 2.0 alpha附带新的中间件,用于管理获取UserManager(app.UseUserManagerFactory设置此实例)的实例,并获取DbContext(app.UseDbContextFactory设置此实例)的实例.有一个例子展示了如何使用MVC应用程序,但没有关于如何从使用OAuthBearerTokens
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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读