asp.net-mvc – 使用ASP.NET Identity 2.0 UserManagerFactory与UseO
发布时间:2020-05-24 05:37:17 所属栏目: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 3 Razor模板VS RenderPartial
- Asp.net mvc – Html提交按钮未发送帖子?
- 在asp.net mvc中可以做一个通用的控制器吗?
- asp.net – 如何更改.ASPX自动格式化设置(Visual Studio)
- asp.net-mvc – 如何使用ASP.NET身份(OWIN)访问Facebook的私
- asp.net-web-api – 如何从ASP.net 5 web api返回文件
- asp.net – 实体框架与存储过程
- 一个ASP.NET Web应用程序中的经典.ASP和.NET .aspx网页
- .net – 编译器错误消息:编译器失败,错误代码为-532462766
- ViewState在ASP.NET MVC中是否相关?
推荐文章
站长推荐
- asp.net-mvc – 如何使用具有可空类型的强类型HT
- asp.net – Html.RenderAction和Html.Action之间
- asp.net-mvc-4 – 在asp.net mvc 4模型中更改验证
- asp.net-mvc – MVC – 如何从get请求获取参数值
- asp.net-mvc – ASP.Net WebAPI区域支持
- ASP.NET MVC3 – 您如何处理探测请求?
- asp.net-mvc – AOP vs MVC FilterAttributes vs
- asp.net-mvc – ASP.NET身份与简单的会员优点和缺
- asp.net-mvc – 如何在Visual Studio 2017中构建
- ASP.NET页面生命周期解释
热点阅读
