Authorize by ClaimIdentity by Owin

來源:互聯網
上載者:User

標籤:vat   persist   think   options   only   mic   ide   ati   package   

Authorize by ClaimIdentity by Owin

  1. Package needed
  • Owin
  • Microsoft.Owin.Security.OAuth
  • Microsoft.Owin.Security.Cookies
  • Microsoft.Owin
  • Microsoft.AspNet.WebApi.Owin
  1. Startup.cs definition
[assembly:OwinStartup(typeof(GoldWebApi.App_Start.Startup))]namespace GoldWebApi.App_Start{    public class Startup    {        public void Configuration(IAppBuilder app)        {        }    }}
  1. By using Cookie
  • Add these function call in startup.cs
app.UseCookieAuthentication(new CookieAuthenticationOptions            {                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,                CookieHttpOnly = false,                CookieName = "Auth",                ExpireTimeSpan = TimeSpan.FromMinutes(1)            });
  • Define this action webapi
 [HttpGet]        public string Login(string userName,string passWord)        {            string realPassword = string.Empty;            if(AccountDic.TryGetValue(userName,out realPassword))            {                if (passWord == realPassword)                {                    this.SignIn(HttpContext.Current.GetOwinContext().Authentication, this.CreateClaimIdentity(userName));                    return "Authenticated";                }            }            return "Deny";        }         private void SignIn(IAuthenticationManager authenticationManger, ClaimsIdentity identity)        {            authenticationManger.SignIn(new AuthenticationProperties()            {                ExpiresUtc = DateTime.UtcNow.AddMinutes(1),                IsPersistent = true            }, identity);        }        private ClaimsIdentity CreateClaimIdentity(string userName)        {            return new ClaimsIdentity(new List<Claim>() { new Claim(ClaimTypes.Name, userName) }, DefaultAuthenticationTypes.ApplicationCookie);        }

4.By Token

  • Add these call in startup.cs
    app.UseOAuthBearerAuthentication(GoldWebApi.Controllers.AccountController.OAuthBearerOptions);
  • Add these definition in webapi
[HttpGet]        public string LoginByTicket(string userName,string passWord)        {            string realPassword = string.Empty;            if (AccountDic.TryGetValue(userName, out realPassword))            {                if (passWord == realPassword)                {                    return this.GenerateTicket(this.CreateClaimIdentity(userName));                }            }            return "Deny";        }        private string GenerateTicket(ClaimsIdentity identity)        {            var ticket = new AuthenticationTicket(identity, new AuthenticationProperties());            ticket.Properties.IssuedUtc = DateTime.Now;            ticket.Properties.ExpiresUtc = DateTime.Now.AddMinutes(1);            return OAuthBearerOptions.AccessTokenFormat.Protect(ticket);        }
  1. By Basic Authentication
  • package install: Thinktecture.IdentityModel.Owin.BasicAuthentication
  • Add these in startup.cs
app.UseBasicAuthentication("localhost", ValidateUserCredential);public Task<IEnumerable<Claim>> ValidateUserCredential(string userName, string passWord)        {            return Task.FromResult<IEnumerable<Claim>>(new List<Claim>() { new Claim(ClaimTypes.Name, userName) });        }

Summary
For all those Authentication mode, we can use Authorize Attribute in our webapi controller/action to apply the Authentication/Authorization. Owin will take the infrustructure job for us.

Authorize by ClaimIdentity by Owin

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.