欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

怎么在ASP.NETCore中實(shí)現(xiàn)一個身份認(rèn)證功能-創(chuàng)新互聯(lián)

怎么在ASP.NET Core中實(shí)現(xiàn)一個身份認(rèn)證功能?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)建站是專業(yè)的榆林網(wǎng)站建設(shè)公司,榆林接單;提供網(wǎng)站建設(shè)、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行榆林網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

創(chuàng)建項(xiàng)目:


在VS中新建項(xiàng)目,項(xiàng)目類型選擇ASP.NET Core Web Application (.NET Core), 輸入項(xiàng)目名稱為TestBasicAuthor。


怎么在ASP.NET Core中實(shí)現(xiàn)一個身份認(rèn)證功能

接下來選擇 Web Application, 右側(cè)身份認(rèn)證選擇:No Authentication

怎么在ASP.NET Core中實(shí)現(xiàn)一個身份認(rèn)證功能

打開Startup.cs

在ConfigureServices方法中加入如下代碼:

services.AddAuthorization();

在Configure方法中加入如下代碼:

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
  AuthenticationScheme = "Cookie", 
  LoginPath = new PathString("/Account/Login"), 
  AccessDeniedPath = new PathString("/Account/Forbidden"), 
  AutomaticAuthenticate = true, 
  AutomaticChallenge = true 
});

完整的代碼應(yīng)該是這樣:


public void ConfigureServices(IServiceCollection services) 
{ 
  services.AddMvc(); 
 
  services.AddAuthorization(); 
} 
 
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
  app.UseCookieAuthentication(new CookieAuthenticationOptions 
  { 
    AuthenticationScheme = "Cookie", 
    LoginPath = new PathString("/Account/Login"), 
    AccessDeniedPath = new PathString("/Account/Forbidden"), 
    AutomaticAuthenticate = true, 
    AutomaticChallenge = true 
  }); 
 
  app.UseMvc(routes => 
  { 
    routes.MapRoute( 
       name: "default", 
       template: "{controller=Home}/{action=Index}/{id?}"); 
  }); 
}

你或許會發(fā)現(xiàn)貼進(jìn)去的代碼是報(bào)錯的,這是因?yàn)檫€沒有引入對應(yīng)的包,進(jìn)入報(bào)錯的這一行,點(diǎn)擊燈泡,加載對應(yīng)的包就可以了。


怎么在ASP.NET Core中實(shí)現(xiàn)一個身份認(rèn)證功能

在項(xiàng)目下創(chuàng)建一個文件夾命名為Model,并向里面添加一個類User.cs

代碼應(yīng)該是這樣

public class User
{
  public string UserName { get; set; }
  public string Password { get; set; }
}

創(chuàng)建一個控制器,取名為:AccountController.cs

在類中貼入如下代碼:


[HttpGet] 
public IActionResult Login() 
{ 
  return View(); 
} 
 
[HttpPost] 
public async Task<IActionResult> Login(User userFromFore) 
{ 
  var userFromStorage = TestUserStorage.UserList 
    .FirstOrDefault(m => m.UserName == userFromFore.UserName && m.Password == userFromFore.Password); 
 
  if (userFromStorage != null) 
  { 
    //you can add all of ClaimTypes in this collection 
    var claims = new List<Claim>() 
    { 
      new Claim(ClaimTypes.Name,userFromStorage.UserName) 
      //,new Claim(ClaimTypes.Email,"emailaccount@microsoft.com") 
    }; 
 
    //init the identity instances 
    var userPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, "SuperSecureLogin")); 
 
    //signin 
    await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal, new AuthenticationProperties 
    { 
      ExpiresUtc = DateTime.UtcNow.AddMinutes(20), 
      IsPersistent = false, 
      AllowRefresh = false 
    }); 
 
    return RedirectToAction("Index", "Home"); 
  } 
  else 
  { 
    ViewBag.ErrMsg = "UserName or Password is invalid"; 
 
    return View(); 
  } 
} 
 
public async Task<IActionResult> Logout() 
{ 
  await HttpContext.Authentication.SignOutAsync("Cookie"); 
 
  return RedirectToAction("Index", "Home"); 
}

相同的文件里讓我們來添加一個模擬用戶存儲的類


//for simple, I'm not using the database to store the user data, just using a static class to replace it.
public static class TestUserStorage
{
  public static List<User> UserList { get; set; } = new List<User>() {
    new User { UserName = "User1",Password = "112233"}
  };
}

接下來修復(fù)好各種引用錯誤。

完整的代碼應(yīng)該是這樣


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TestBasicAuthor.Model;
using System.Security.Claims;
using Microsoft.AspNetCore.Http.Authentication;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace TestBasicAuthor.Controllers
{
  public class AccountController : Controller
  {
    [HttpGet]
    public IActionResult Login()
    {
      return View();
    }

    [HttpPost]
    public async Task<IActionResult> Login(User userFromFore)
    {
      var userFromStorage = TestUserStorage.UserList
        .FirstOrDefault(m => m.UserName == userFromFore.UserName && m.Password == userFromFore.Password);

      if (userFromStorage != null)
      {
        //you can add all of ClaimTypes in this collection 
        var claims = new List<Claim>()
        {
          new Claim(ClaimTypes.Name,userFromStorage.UserName) 
          //,new Claim(ClaimTypes.Email,"emailaccount@microsoft.com") 
        };

        //init the identity instances 
        var userPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, "SuperSecureLogin"));

        //signin 
        await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal, new AuthenticationProperties
        {
          ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
          IsPersistent = false,
          AllowRefresh = false
        });

        return RedirectToAction("Index", "Home");
      }
      else
      {
        ViewBag.ErrMsg = "UserName or Password is invalid";

        return View();
      }
    }

    public async Task<IActionResult> Logout()
    {
      await HttpContext.Authentication.SignOutAsync("Cookie");

      return RedirectToAction("Index", "Home");
    }
  }

  //for simple, I'm not using the database to store the user data, just using a static class to replace it.
  public static class TestUserStorage
  {
    public static List<User> UserList { get; set; } = new List<User>() {
    new User { UserName = "User1",Password = "112233"}
  };
  }
}

在Views文件夾中創(chuàng)建一個Account文件夾,在Account文件夾中創(chuàng)建一個名位index.cshtml的View文件。

貼入如下代碼:


@model TestBasicAuthor.Model.User

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
</head>
<body>
  @using (Html.BeginForm())
  {
    <table>
      <tr>
        <td></td>
        <td>@ViewBag.ErrMsg</td>
      </tr>
      <tr>
        <td>UserName</td>
        <td>@Html.TextBoxFor(m => m.UserName)</td>
      </tr>
      <tr>
        <td>Password</td>
        <td>@Html.PasswordFor(m => m.Password)</td>
      </tr>
      <tr>
        <td></td>
        <td><button>Login</button></td>
      </tr>
    </table>
  }
</body>
</html>

打開HomeController.cs

添加一個Action, AuthPage.


[Authorize]
[HttpGet]
public IActionResult AuthPage()
{
  return View();
}

在Views/Home下添加一個視圖,名為AuthPage.cshtml


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
</head>
<body>
  <h2>Auth page</h2>

  <p>if you are not authorized, you can't visit this page.</p>
</body>
</html>

到此,一個基礎(chǔ)的身份認(rèn)證就完成了,核心登陸方法如下:


await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal, new AuthenticationProperties
{
  ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
  IsPersistent = false,
  AllowRefresh = false
});

啟用驗(yàn)證如下:


public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
  app.UseCookieAuthentication(new CookieAuthenticationOptions
  {
    AuthenticationScheme = "Cookie",
    LoginPath = new PathString("/Account/Login"),
    AccessDeniedPath = new PathString("/Account/Forbidden"),
    AutomaticAuthenticate = true,
    AutomaticChallenge = true
  });
}

關(guān)于怎么在ASP.NET Core中實(shí)現(xiàn)一個身份認(rèn)證功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

網(wǎng)站題目:怎么在ASP.NETCore中實(shí)現(xiàn)一個身份認(rèn)證功能-創(chuàng)新互聯(lián)
標(biāo)題來源:http://aaarwkj.com/article24/ccogje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、網(wǎng)站收錄手機(jī)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)外貿(mào)建站、網(wǎng)頁設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司
av天堂资源在线播放| 中文字幕人妻紧贴拍摄| 精品亚洲第一区二区免费在线| 内射极品美女在线观看| 粗暴蹂躏中文一区二区三区| 女同伦理视频在线观看| 欧美一区二区日韩一区二区| 成年人正常性生活频率| 亚洲天堂,男人的天堂| 日本一区二区三区不卡在线| 激情欧美精品桃桃激情| 成人欧美一区二区三区av| 丰满人妻二区三区性色| 禁止18岁以下的视频| 日韩在线免费色视频| 日本免费一区二区三区视频观看| 亚洲欧美二区中文字幕| 国产特级黄色片免费看| 91麻豆精品国产自产| 久久亚洲精品中文字幕一| 久久三级中文欧大战字幕| 成人午夜激情四射av| 欧美激情片免费在线观看| 久久五月婷婷爱综合亚洲| 91精品国产综合久久麻豆| 成人大片在线免费观看视频| 亚洲黄色一区大陆av剧情| 日韩精品女性三级视频| 五月婷婷六月丁香在线观看| 日韩欧美中文在线一区二区| 国产有码日产一区在线观看| 国产精品妇女一二三区| 日本三本道成人免费毛片| 日本欧美激情在线观看| 白小白的视频在线观看| 欧美久久久久综合一区| av网址在线免费观看| 白白色发布青青在线视频观看| 日韩不卡区高清在线视频| 青娱乐青青草91在线| 日本在线一区二区三区免费视频|