【发布时间】:2014-07-06 21:30:21
【问题描述】:
我想实现自己的自定义 WebApi 身份验证。我尝试在 ApplicationOAuthProvider 中修改 GrantResourceOwnerCredentials 方法。
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
using (UserManager<IdentityUser> userManager = _userManagerFactory())
{
IdentityUser 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 userManager.CreateIdentityAsync(user,
context.Options.AuthenticationType);
ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
}
我替换认证:
IdentityUser user = await userManager.FindAsync(context.UserName, context.Password);
我的 Web 服务调用返回 true 表示已通过身份验证,返回 false 表示未通过身份验证。
if (authenticated)
{
IdentityUser user = new IdentityUser("username");
}
但我不知道如何处理 ClaimsIdentity。谁有我可以参考的样品?谢谢。
【问题讨论】:
标签: c# authentication asp.net-web-api owin