博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Web API2 使用默认Identity
阅读量:4686 次
发布时间:2019-06-09

本文共 7109 字,大约阅读时间需要 23 分钟。

当您选择个人账户在Web API项目模板,项目包含一个令牌授权服务器验证用户凭证和问题。下面的图显示了相同的凭证流的Web API组件。

发送一个未经授权的请求

首先,运行应用程序并单击按钮调用的API。当请求完成后,您应该看到一条错误消息在结果框。这是因为要求不包含一个访问令牌,所以请求授权。

调用API按钮发送一个AJAX请求~/api/values,调用一个Web API控制器动作。这是部分发送AJAX请求的JavaScript代码。在样例应用程序中,所有的JavaScript应用程序代码位于Scripts\app.js file文件。

JavaScript
// If we already have a bearer token, set the Authorization header.var token = sessionStorage.getItem(tokenKey);var headers = {};if (token) {    headers.Authorization = 'Bearer ' + token; } $.ajax({ type: 'GET', url: 'api/values/1', headers: headers }).done(function (data) { self.result(data); }).fail(showError);

直到用户登录,没有不记名的令牌,因此没有授权请求头。这使请求返回一个401错误。

Here is the HTTP request. 

console
GET https://localhost:44305/api/values HTTP/1.1Host: localhost:44305User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0Accept: */* Accept-Language: en-US,en;q=0.5 X-Requested-With: XMLHttpRequest Referer: https://localhost:44305/

HTTP response:

console
HTTP/1.1 401 UnauthorizedContent-Type: application/json; charset=utf-8Server: Microsoft-IIS/8.0WWW-Authenticate: Bearer Date: Tue, 30 Sep 2014 21:54:43 GMT Content-Length: 61 { "Message":"Authorization has been denied for this request."}

注册用户

发送一个POST 请求到 ~/api/Account/Register/. body是已个json对象. Here is the JavaScript code that sends the request:

Copy
JavaScript
var data = {    Email: self.registerEmail(),    Password: self.registerPassword(),    ConfirmPassword: self.registerPassword2()};$.ajax({    type: 'POST', url: '/api/Account/Register', contentType: 'application/json; charset=utf-8', data: JSON.stringify(data) }).done(function (data) { self.result("Done!"); }).fail(showError);

HTTP request:

console
POST https://localhost:44305/api/Account/Register HTTP/1.1Host: localhost:44305User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0Accept: */* Content-Type: application/json; charset=utf-8 X-Requested-With: XMLHttpRequest Referer: https://localhost:44305/ Content-Length: 84 { "Email":"alice@example.com","Password":"Password1!","ConfirmPassword":"Password1!"}

HTTP response:

console
HTTP/1.1 200 OKServer: Microsoft-IIS/8.0Date: Wed, 01 Oct 2014 00:57:58 GMTContent-Length: 0

这个请求是由AccountController处理类。在内部,AccountController使用ASP.NET身份管理成员数据库。

如果你在本地运行应用程序从Visual Studio用户帐户存储在LocalDB,AspNetUsers表。查看表在Visual Studio中,单击视图菜单,选择Server Explorer,然后扩大数据连接。

得到一个访问Token

到目前为止,我们还没有做任何OAuth,但现在我们将看到OAuth授权服务器,当我们请求一个访问令牌。示例应用程序的登录区域,输入电子邮件和密码,点击登录。

The Log In button sends a request to the token endpoint. The body of the request contains the following form-url-encoded data:

  • grant_type: "password"
  • username: <the user's email>
  • password: <password>

Here is the JavaScript code that sends the AJAX request:

JavaScript
var loginData = {    grant_type: 'password',    username: self.loginEmail(),    password: self.loginPassword() }; $.ajax({ type: 'POST', url: '/Token', data: loginData }).done(function (data) { self.user(data.userName); // Cache the access token in session storage. sessionStorage.setItem(tokenKey, data.access_token); }).fail(showError);

HTTP request:

console
POST https://localhost:44305/Token HTTP/1.1Host: localhost:44305User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0Accept: */* Content-Type: application/x-www-form-urlencoded; charset=UTF-8 X-Requested-With: XMLHttpRequest Referer: https://localhost:44305/ Content-Length: 68 grant_type=password&username=alice%40example.com&password=Password1!

HTTP response:

console
HTTP/1.1 200 OKContent-Length: 669Content-Type: application/json;charset=UTF-8Server: Microsoft-IIS/8.0Date: Wed, 01 Oct 2014 01:22:36 GMT { "access_token":"imSXTs2OqSrGWzsFQhIXziFCO3rF...", "token_type":"bearer", "expires_in":1209599, "userName":"alice@example.com", ".issued":"Wed, 01 Oct 2014 01:22:33 GMT", ".expires":"Wed, 15 Oct 2014 01:22:33 GMT" }

发送一个身份验证请求

现在我们有一个不记名的令牌,我们可以做出一个身份验证请求API。这是通过设置授权请求头。单击按钮再次看到这个调用API。

HTTP request:

console
GET https://localhost:44305/api/values/1 HTTP/1.1Host: localhost:44305User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0Accept: */* Authorization: Bearer imSXTs2OqSrGWzsFQhIXziFCO3rF... X-Requested-With: XMLHttpRequest

HTTP response:

console
HTTP/1.1 200 OKContent-Type: application/json; charset=utf-8Server: Microsoft-IIS/8.0Date: Wed, 01 Oct 2014 01:41:29 GMTContent-Length: 27 "Hello, alice@example.com."

Log Out

因为浏览器不缓存凭证或访问令牌,退出是一个简单的问题“忘记”的令牌,通过移除它从会话存储:

JavaScript
self.logout = function () {    sessionStorage.removeItem(tokenKey)}

理解个人账户项目模板

When you select Individual Accounts in the ASP.NET Web Application project template, the project includes:

  • An OAuth2 authorization server.
  • A Web API endpoint for managing user accounts
  • An EF model for storing user accounts.

Here are the main application classes that implement these features:

  • AccountControlle r. Provides a Web API endpoint for managing user accounts. The Register action is the only one that we used in this tutorial. Other methods on the class support password reset, social logins, and other functionality.
  • ApplicationUser, defined in /Models/IdentityModels.cs. This class is the EF model for user accounts in the membership database.
  • ApplicationUserManager, defined in /App_Start/IdentityConfig.cs This class derives from  and performs operations on user accounts, such as creating a new user, verifying passwords, and so forth, and automatically persists changes to the database.
  • ApplicationOAuthProvider. This object plugs into the OWIN middleware, and processes events raised by the middleware. It derives from .

Configuring the Authorization Server

In StartupAuth.cs, the following code configures the OAuth2 authorization server.

Copy
C#
PublicClientId = "self";OAuthOptions = new OAuthAuthorizationServerOptions{    TokenEndpointPath = new PathString("/Token"),    Provider = new ApplicationOAuthProvider(PublicClientId), AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), // Note: Remove the following line before you deploy to production: AllowInsecureHttp = true }; // Enable the application to use bearer tokens to authenticate users app.UseOAuthBearerTokens(OAuthOptions);

The TokenEndpointPath property is the URL path to the authorization server endpoint. That's the URL that app uses to get the bearer tokens.

The Provider property specifies a provider that plugs into the OWIN middleware, and processes events raised by the middleware.

Here is the basic flow when the app wants to get a token:

  1. To get an access token, the app sends a request to ~/Token.
  2. The OAuth middleware calls GrantResourceOwnerCredentials on the provider.
  3. The provider calls the ApplicationUserManager to validate the credentials and create a claims identity.
  4. If that succeeds, the provider creates an authentication ticket, which is used to generate the token.

The OAuth middleware doesn't know anything about the user accounts. The provider communicates between the middleware and ASP.NET Identity. For more information about implementing the authorization server

 

转载于:https://www.cnblogs.com/Javi/p/6420117.html

你可能感兴趣的文章
hdu-1814(2-sat)
查看>>
谷歌浏览器,添加默认搜索引擎的搜索地址
查看>>
数据结构化与保存
查看>>
如何避免在简单业务逻辑上面的细节上面出错
查看>>
Linux shell 命令判断执行语法 ; , && , ||
查看>>
vim代码格式化插件clang-format
查看>>
RTP Payload Format for Transport of MPEG-4 Elementary Streams over http
查看>>
Java环境变量设置
查看>>
【JBPM4】判断节点decision 方法3 handler
查看>>
filter 过滤器(监听)
查看>>
node启动时, listen EADDRINUSE 报错;
查看>>
杭电3466————DP之01背包(对状态转移方程的更新理解)
查看>>
kafka中的消费组
查看>>
python--注释
查看>>
小组成员及其git链接
查看>>
SQL case when else
查看>>
MVc Identity登陆锁定
查看>>
cdn连接失败是什么意思_关于CDN的原理、术语和应用场景那些事
查看>>
ultraedit26 运行的是试用模式_免费试用U盘数据恢复工具 – 轻松找回U盘丢失的各种数据!...
查看>>
python sum函数导入list_python sum函数iterable参数为二维list,start参数为“[]”该如何理解...
查看>>