Asp.net web api 在Web Api/Owin体系结构中,对'/代币';处理好了吗?

Asp.net web api 在Web Api/Owin体系结构中,对'/代币';处理好了吗?,asp.net-web-api,owin,katana,Asp.net Web Api,Owin,Katana,我试图了解Asp.net Web Api个人帐户的身份验证和授权。我在网上看到了一些教程,包括。简言之,当用户代理提供用户名和密码时,API将发出一个令牌,客户端将在对API的后续调用中使用该令牌来标识自己。用户代理通过发出请求来接收令牌,通常为:。路径似乎是在Startup类中设置的,如下所示: TokenEndpointPath = new PathString("/Token") 我的问题是,我找不到任何与该路径匹配的控制器方法。这是如何工作的?当您在ASP.NET中创建具有单独身份验证

我试图了解Asp.net Web Api个人帐户的身份验证和授权。我在网上看到了一些教程,包括。简言之,当用户代理提供用户名和密码时,API将发出一个令牌,客户端将在对API的后续调用中使用该令牌来标识自己。用户代理通过发出请求来接收令牌,通常为:。路径似乎是在Startup类中设置的,如下所示:

TokenEndpointPath = new PathString("/Token")

我的问题是,我找不到任何与该路径匹配的控制器方法。这是如何工作的?

当您在ASP.NET中创建具有单独身份验证的新项目时,将使用OAuth提供程序创建解决方案,以处理身份验证请求

如果查看您的解决方案,您应该会看到一个带有类ApplicationAuthProvider的Providers文件夹

此类实现了对网站中的成员进行身份验证的所有逻辑。 该配置在启动时设置,以允许您通过OAuthOption自定义url端点

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/Token"),
    Provider = new ApplicationOAuthProvider(PublicClientId),
    AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
    AllowInsecureHttp = true
};
TokenEndPoint路径属性定义了将触发GrandResourceOwnerCredentials的GrantResourceOwnerCredentials方法的url

如果您使用fiddler来验证和使用这种主体

 grant_type=password&username=testUserName&password=TestPassword
您应该通过以下方法传递:

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,
            CookieAuthenticationDefaults.AuthenticationType);

        AuthenticationProperties properties = CreateProperties(user.UserName);
        AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
        context.Validated(ticket);
        context.Request.Context.Authentication.SignIn(cookiesIdentity);
    }
public override异步任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext)
{
var userManager=context.OwinContext.GetUserManager();
ApplicationUser user=await userManager.FindAsync(context.UserName,context.Password);
if(user==null)
{
SetError(“无效的授权”,“用户名或密码不正确”);
返回;
}
ClaimsIdentity oAuthIdentity=等待用户.GenerateUserIdentityAsync(userManager,
OAuthDefaults.AuthenticationType);
ClaimSideEntity cookiesIdentity=等待用户.GenerateUserIdentity异步(userManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties=CreateProperties(user.UserName);
AuthenticationTicket=新的AuthenticationTicket(OAuthidentitity,属性);
上下文。已验证(票证);
context.Request.context.Authentication.sign(cookiesIdentity);
}
其中context.UserName和context.Password与请求中使用的数据一起设置。 确认身份后(这里使用实体框架和数据库中的一对用户名、密码),将向调用者发送一个承载令牌。 然后可以使用该承载令牌对其他呼叫进行身份验证


问候。

@Jeremie,谢谢你澄清此事。非常好的解释,这正是我的问题。有谁能告诉我,使用这个默认的oauth承载令牌是否是在API中实现安全性的最佳和最安全的方法?我正忙于开发一个API,该API将公开于互联网,并被我目前正在开发的angularjs移动应用程序所使用。还有其他选择吗?非常感谢。@Jeremie,有可能有多个端点URL都指向同一个IAppBuilder应用程序。如果不是,我们如何在一个web api应用程序中拥有多个toke端点?