zoukankan      html  css  js  c++  java
  • Authencation WebApi Learning

    Learning Note About Web Authentication and Authorize

    1.we use Owin to implement the Authentication and Authorize.

    we create a new Startup.cs file to replace the global.asax file. here is a general content of the startup.cs file.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using Microsoft.Owin;
    using Owin;
    using Microsoft.Owin.Security.OAuth;
    using angularjsAuthentication.api.Providers;
    
    [assembly:OwinStartup(typeof(angularjsAuthentication.api.Startup))]
    namespace angularjsAuthentication.api
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                HttpConfiguration config = new HttpConfiguration();
    
                ConfigureOAuth(app);
    
                WebApiConfig.Register(config);
                app.UseWebApi(config);
            }
    
            public void ConfigureOAuth(IAppBuilder app)
            {
                OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
                {
                    AllowInsecureHttp = true,
                    TokenEndpointPath = new PathString("/token"),
                    AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                    Provider = new SimpleAuthorizationServerProvider(),
                    RefreshTokenProvider = new SimpleRefreshTokenProvider()
                };
    
                app.UseOAuthAuthorizationServer(OAuthServerOptions);
                app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
            }
        }
    }
    
    1. Some key class : OAuthAuthorizationServerOptions
      OAuthAuthorizationServerProvider

    we have an important interface IOAuthAuthorizationServerProvider, the OAuthAuthorizationServerOptions provide a default implementation of this interface.
    if we have any custom requirement, we can inherite it and override some methods.

    2.1 For the first method OAuthAuthorizationServerProvider.ValidateClientAuthentication(), the key point, if validate pass, call context.Validate(), otherwise, call context.setErrors().

    2.2 For this class, take care of these methods OAuthAuthorizationServerProvider.GrantResourceOwnerCredentials
    this method is reponsible for grantting access token to the request with grant_type as password, if success, call context.validate(token). generally, if a request arrives at token Endpoint with grant_type password, this method will be called.

    these sub class AuthenticationTicket, ClaimsIdentity. AuthenticationProperties

    2.3 OAuthAuthorizationServerProvider.GrantRefreshToken, called when a request to tokenendpoint with grant_type refresh_token. we can see the http api.

    3. OAuth2

    3.1 we have a lot of high quality articles descriping this protocol, here is just a link: link1, we can get a lot from cnblogs.
    here we just make things simple, OAuth2 support four types of Authorization granttypes: Authorization Code Grant, Implicit Grant, Resource Owener Password Credentials Grant, Client Credential Grant. For each Authorization granttype, we make a note of each method called during a end2end test.

    3.1 Resource Owener Password Credentials Grant

    first, we request the access token, this method will be called OAuthAuthorizationServerProvider.ValidateClientAuthentication, this function is called to validate if the client is a registered client. if passed, OAuthAuthorizationServerProvider.GrantResourceOwnerCredentials will be called. see the msdn .

    secondly, if we provide the RefreshTokenProvider which implete the interface IAuthenticationTokenProvider, if user request an access token, the workflow will show like this: OAuthAuthorizationServerProvider.ValidateClientAuthentication -> OAuthAuthorizationServerProvider.GrantResourceOwnerCredentials -> IAuthenticationTokenProvider.CreateAsync -> OAuthAuthorizationServerProvider.TokenEndpoint; if user try to refresh the access token, the workflow will like this: OAuthAuthorizationServerProvider.ValidateClientAuthentication ->
    OAuthAuthorizationServerProvider.GrantRefreshToken -> IAuthenticationTokenProvider.ReceiveAsync -> OAuthAuthorizationServerProvider.TokenEndpoint

    3.2 Authorization Code Grant

  • 相关阅读:
    两种常用图像识别迁移学习方法
    学习备忘
    图像处理代码举例(C++、MATLAB、OpenCV)
    Java 读取clob字段的几种方法
    Oracle 查询表注释以及字段注释
    Oracle 查询表的索引包含的字段
    Oracle 获取表的主键、外键以及唯一约束条件
    Oracle列信息表 all_tab_columns中的data_length和data_precision字段区别
    Oracle 表结构、索引以及分区信息查询
    Java中的String,StringBuilder,StringBuffer三者的区别
  • 原文地址:https://www.cnblogs.com/kongshu-612/p/8579559.html
Copyright © 2011-2022 走看看