Ben*_*iFB 12 asp.net-mvc owin asp.net-mvc-5 asp.net-identity
在ASP.NET 2.0身份阿尔法附带了新的中间件来管理得到的一个实例UserManager(app.UseUserManagerFactory设置此)和获得的一个实例DbContext(app.UseDbContextFactory设置此).有一个示例显示如何使用MVC应用程序,但没有关于如何从使用的SPA模板获得此工作的文档OAuthBearerTokens,与示例不同.
我目前陷入困境:
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);
Run Code Online (Sandbox Code Playgroud)
并且不知道如何UserManagerFactory使用2.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
}
});
Run Code Online (Sandbox Code Playgroud)
谢谢... - 贝恩
小智 13
我在这里添加存根,向您展示如何使用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),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
// namespace below needed to enable GetUserManager extension of the OwinContext
using Microsoft.AspNet.Identity.Owin;
Run Code Online (Sandbox Code Playgroud)
hyl*_*er0 13
ASP.NET标识包括支持创建单个实例UserManager和DBContext每个应用程序请求的标识.要支持此模式,请对每个IAppBuilder对象使用以下扩展方法:
app.CreatePerOwinContext<AppUserIdentityDbContext>(AppUserIdentityDbContext.Create);
app.CreatePerOwinContext<AppUserManager>(AppUserManager.Create);
Run Code Online (Sandbox Code Playgroud)
您可以在下面找到实现此模式的一个很好的示例:
这是AppManager类:
public class AppUserManager : UserManager<AppUserIdentity>
{
public AppUserManager(IUserStore<AppUserIdentity> store)
: base(store) { }
public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options, IOwinContext context)
{
var manager = new AppUserManager(new UserStore<AppUserIdentity>(context.Get<AppUserIdentityDbContext>()));
return manager;
}
}
Run Code Online (Sandbox Code Playgroud)
本文使用OWIN Middleware组件UseOAuthBearerAuthentication并UseCookieAuthentication支持基于浏览器的身份验证以及单个Owin上下文IdentityDb对象和单个AppManager.
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
//This will used the HTTP header: "Authorization" Value: "Bearer 1234123412341234asdfasdfasdfasdf"
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
// Enable the application to use a cookie to store information for the signed in user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
Run Code Online (Sandbox Code Playgroud)
HostAuthenticationFilter表示通过OWIN中间件进行身份验证的身份验证过滤器:
config.SuppressDefaultHostAuthentication();
//This will used the HTTP header: "Authorization" Value: "Bearer 1234123412341234asdfasdfasdfasdf"
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
Run Code Online (Sandbox Code Playgroud)
var identity = new ClaimsIdentity(Startup.OAuthBearerOptions.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, user));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userIdentity.Id));
AuthenticationTicket ticket = new AuthenticationTicket(identity, new AuthenticationProperties());
var currentUtc = new SystemClock().UtcNow;
ticket.Properties.IssuedUtc = currentUtc;
ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30));
string AccessToken = Startup.OAuthBearerOptions.AccessTokenFormat.Protect(ticket);
return AccessToken;
Run Code Online (Sandbox Code Playgroud)
Ben,其中一些已经从alpha1改为beta1版本(目前可在ASP.NET Nightly NuGet Repo上获得,网址为https://aspnetwebstack.codeplex.com/wikipage?title=Use%20Nightly%20Builds).如果您升级到最新的beta位,您将不再使用此语法,而是:
// Configure the db context and user manager to use per request
app.CreatePerOwinContext(ApplicationIdentityContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
Run Code Online (Sandbox Code Playgroud)
另外,请注意HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>现在转移到Microsoft.AspNet.Identity.Owin.
您可以安装`Microsoft.AspNet.Identity.Samples'包(最好是在新的MVC项目中,因为它可能会覆盖文件).它帮助我了解他们如何做某些事情,考虑到2.0的文档目前还不存在,除了一些博客文章(所有这些都是为alpha1版本编写的).
| 归档时间: |
|
| 查看次数: |
25151 次 |
| 最近记录: |