web Api 2移动身份验证器

时间:2014-01-09 23:09:48

标签: c# javascript jquery asp.net html5

我在Web API2中有一个服务器,带有Owin承载认证令牌。

config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

如何使用/启用Mobile Native App使用Web API2服务进行身份验证。

1 个答案:

答案 0 :(得分:0)

注册用户:

Send a POST request to /api/account/register with:
header: Content-Type: application/json
body: { 'UserName': 'xxx', 'Password': 'yyy', 'ConfirmPassword': 'yyy' }

现在您已经注册了用户,您必须“登录”并获取一个令牌,您将在每个后续请求中使用该令牌来识别发出请求的用户:

Send a POST request to /token with:
header Content-Type: application/x-www-form-urlencoded
body: { grant_type=password&username=Alice&password=password123 }

这将返回类似于此的JSON:

{
    'access_token':'boQtj0SCGz2GFGz,
    'token_type':'bearer',
    'expires_in':1209599,
    'userName':'xxx',
    '.issued":'Mon, 14 Oct 2013 06:53:32 GMT',
    '.expires":'Mon, 28 Oct 2013 06:53:32 GMT'
}

您需要识别访问令牌以识别发出请求的用户,以便将其存储在某处。当您以用户'xxx'发出另一个请求时,您需要将该标记包含为如下标题:

Authorization: Bearer boQtj0SCGz2GFGz

请注意,令牌会更长。这里的所有都是它的。每次发出请求时,您只需要将令牌作为标题包含。

I found this info here. It's a good read.