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 } }); 谢谢… 解决方法
我在这里添加存根,显示如何使用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-mvc – ASP.NET MVC向最终用户显示操作成功
- asp.net-mvc – 将JSON.NET JObject转换为JsonResult的异常
- asp.net – Http错误503的自定义错误页面
- asp.net – 在VS Code中指定localhost端口的位置
- asp.net – 从MVC视图中的模型访问displayName属性
- ASP.NET MembershipProvider加密/解密
- asp.net – jQuery弹出窗口返回父级的值
- asp.net – Web部署不部署index.cshtml
- 我如何在ASP.NET MVC中使用Application_Error?
- Asp.Net – 什么是<%$?
推荐文章
站长推荐
- asp.net – If-Modified-由于IE9传递的HTTP头包含
- asp.net-mvc – 为什么MVC提供的Default Account
- asp.net-mvc – 允许操作和视图和模型的高度变化
- 403发布asp.net MVC后禁止
- asp.net-mvc – 如何调试此错误:’无法找到iise
- asp.net-mvc – ASP.NET MVC局部视图慢?
- asp.net – 如何在web.config中读取会话状态信息
- asp-classic – Classic ASP中的Response.Flush导
- asp.net-mvc-3 – 如何将单选按钮与ASP.Net MVC中
- asp.net – 单语句VB.NET条件为false时执行if-bl
热点阅读