前言
看了教程1以后,很多人問道Client的里面那些配置項是什么意思,希望了解更多的理論知識。但是我覺得IT雖然是理工科,確是一個實踐科學,需要很多感性認識。學習IT最好的辦法是動手去做,所謂守破離(Shu Ha Ri),首先要模仿得到足夠的實踐知識,然后是了解理論,然后是突破! 當然,如果現(xiàn)在就想知道理論,可以參看Identity Server3 教程目錄(http://www.lxweimin.com/p/398b226e41ed),雖然是ID3的介紹,但是理論沒什么變化。
按照IdentityServer4使用教程1,我們已經(jīng)得到了一個ID4的服務器了。如果不想按照前面的步驟創(chuàng)建工程,可以用下面的代碼clone出step-1代碼:
git clone -b step-1 https://github.com/miemengniao/ID4.Learn.git
當然,也可以直接clone出本教程完成后的代碼git clone -b step-2 https://github.com/miemengniao/ID4.Learn.git
準備工作
我們首先創(chuàng)建一個MVC客戶端,然后創(chuàng)建一個solution,把之前的ID4 Server和這個MVC 客戶端加入到這個解決方案中。
- 創(chuàng)建一個MVC客戶端
dotnet new mvc --auth None -o ID4.MvcClient
,注意:我們這里面選則--auth None
- 轉(zhuǎn)到
ID4.MvcClient
目錄并添加所需要的包
cd ID4.MvcClient
dotnet add package Microsoft.AspNetCore.Authentication.Cookies --version 1.1.2
dotnet add package Microsoft.AspNetCore.Authentication.OpenIdConnect --version 1.1.2
- 我們回到根目錄,創(chuàng)建一個solution,并添加ID4.Server和ID4.MvcClient到這個解決方案中,并編譯這個工程
cd ..
dotnet new sln -n ID4.Learn
dotnet sln add .\ID4.Server\ID4.Server.csproj
dotnet sln add .\ID4.MvcClient\ID4.MvcClient.csproj
dotnet restore
dotnet build
修改MVC客戶端支持ID4
增加一個受控頁面
- 在
.\ID4.MvcClient\Views\Home
目錄下創(chuàng)建一個Auth.cshtml
文件,來顯示將來登陸用戶的聲明(claims
)信息。
<dl>
@foreach (var claim in User.Claims)
{
<dt>@claim.Type</dt>
<dd>@claim.Value</dd>
}
</dl>
可以通過
curl -k -O https://raw.githubusercontent.com/miemengniao/ID4.Learn/step-2/ID4.MvcClient/Views/Home/Auth.cshtml
直接下載
- 修改
.\ID4.MvcClient\Views\Shared\_Layout.cshtml
文件,在導航欄增加Auth的超鏈接
<li><a asp-area="" asp-controller="Home" asp-action="Auth">Auth</a></li>
可以通過
curl -k -O https://raw.githubusercontent.com/miemengniao/ID4.Learn/step-2/ID4.MvcClient/Views/Shared/_Layout.cshtml
下載更新后的_Layout文件
- 修改```.\ID4.MvcClient\Controllers\HomeController.cs文件,增加Auth對應的Action
[Authorize]
public IActionResult Auth()
{
return View();
}
可以通過
curl -k -O https://raw.githubusercontent.com/miemengniao/ID4.Learn/step-2/ID4.MvcClient/Controllers/HomeController.cs
下載更新后的HomeController.cs文件
修改Startup文件,增加ID4的授權(quán)認證
- 在startup的Configure方法的UseStaticFiles的調(diào)用后面,加上如下代碼,訪問ID4.Server進行認證:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies"
});
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = "oidc",
SignInScheme = "Cookies",
Authority = "http://localhost:5000",
RequireHttpsMetadata = false,
ClientId = "mvc",
ClientSecret = "secret",
ResponseType = "code id_token",
GetClaimsFromUserInfoEndpoint = true,
SaveTokens = true
});
可以通過
curl -k -O https://raw.githubusercontent.com/miemengniao/ID4.Learn/step-2/ID4.MvcClient/Startup.cs
下載更新后的Startup.cs文件. 如果是手動添加代碼,注意要添加using語句來導入命名空間
- 更新Program.cs文件,來指定啟動端口為5002.
.UseUrls("http://localhost:5002/")
可以通過
curl -k -O https://raw.githubusercontent.com/miemengniao/ID4.Learn/step-2/ID4.MvcClient/Program.cs
下載更新后的Startup.cs文件. 如果是手動添加代碼,注意要添加using語句來導入命名空間
- 啟動ID4 Server和MVC客戶端
因為dotnet CLI不能通過solution文件同時啟動兩個項目,所以需要打開兩個cmd界面,分別進入到ID4.Server和ID4.MvcClient目錄,運行dotnet run
啟動好以后,在瀏覽器重輸入http://localhost:5002,并點擊Auth鏈接,瀏覽器跳轉(zhuǎn)到localhost:5000要求登陸。登陸后,即可看到如下頁面,登陸用戶的所有claim(聲明)信息將列出。
如果之前沒有再localhost:5000創(chuàng)建用戶,可能需要預先在localhost:5000創(chuàng)建好測試用戶
下一節(jié),我們會配置一個Web API來使用這個ID4認證服務器,敬請期待 ??