我不熟悉Azure Web身份验证,想知道我做错了什么吗?
首页/索引是默认路由。
我有此功能来捕获/.auth/me信息:
var mobileClient = new WindowsAzure.MobileServiceClient(functionAppBaseUrl);
$(document).ready(function () {
mobileClient.invokeApi(`${functionAppBaseUrl}.auth/me`,
{
method: 'GET',
headers: {
'accept': 'application/json',
'content-type': 'application/json'
}
})
.then(function (response) {
console.log(`Response from .auth/me: ${response.responseText}`);
$.ajax({
type: "POST",
url: '@Url.Action("Auth", "Home")',
dataType: 'json',
data: { login: JSON.stringify(response) },
success: function () {
console.log("Success Post");
},
error: function () {
console.log("Post Failed");
}
});
}, function (error) {
console.log(`Error from .auth/me: ${JSON.stringify(error)}`);
});
});
然后成功,它将数据发布到家庭控制器中的Auth / Home。
[HttpPost]
public IActionResult Auth(ExternalLogin login)
{
string userRole;
var role = _context.Employees.Where(x => x.id == login.id)
.Select(x => x.HrFlag)
.FirstOrDefault();
if (role == true)
userRole = "hr";
else
userRole = "employee";
var empId = _context.Employees.Where(x => x.id== login.id)
.Select(x => x.EmployeeId)
.FirstOrDefault();
HttpContext.Session.SetString("user_id", login.id);
HttpContext.Session.SetString("expiry_on", login.ExpiresOn.ToShortTimeString());
HttpContext.Session.SetString("access_token", login.AccessToken);
HttpContext.Session.SetString("user_role", userRole);
HttpContext.Session.SetString("empId", empId.ToString());
return View(nameof(HomeHr));
}
然后,它获取ajax调用(从/ auth / me)传递到模型中的值,然后设置会话变量。然后,我在layout.cshtml和home.cshtml
中称它们为layout.cshtml:
@using Microsoft.AspNetCore.Http;
@inject IHttpContextAccessor HttpContextAccessor
@{ var empId = Context.Session.GetString("empId");}
<a class="dropdown-item" href="@Url.Action("EditHr", "Home", new { id = empId })">My Profile</a>
@if (Context.Session.GetString("user_role") == "hr")
{
<a class="dropdown-item" href="@Url.Action("employees", "Home")">Employees</a>
外部登录模型
public class ExternalLogin
{
[JsonProperty("access_token", NullValueHandling = NullValueHandling.Ignore)]
public string AccessToken { get; set; }
[JsonProperty("provider_name", NullValueHandling = NullValueHandling.Ignore)]
public string ProviderName { get; set; }
[JsonProperty("user_id", NullValueHandling = NullValueHandling.Ignore)]
public string Id{ get; set; }
[JsonProperty("user_claims", NullValueHandling = NullValueHandling.Ignore)]
public AuthUserClaim[] UserClaims { get; set; }
[JsonProperty("access_token_secret", NullValueHandling = NullValueHandling.Ignore)]
public string AccessTokenSecret { get; set; }
[JsonProperty("authentication_token", NullValueHandling = NullValueHandling.Ignore)]
public string AuthenticationToken { get; set; }
[JsonProperty("expires_on", NullValueHandling = NullValueHandling.Ignore)]
public DateTime ExpiresOn { get; set; }
[JsonProperty("id_token", NullValueHandling = NullValueHandling.Ignore)]
public string IdToken { get; set; }
[JsonProperty("refresh_token", NullValueHandling = NullValueHandling.Ignore)]
public string RefreshToken { get; set; }
}
我要实现的一般流程: 当用户访问azure托管网站时:用户登录,重定向到我的应用主目录/索引,/。auth / me捕获并显示在控制台中,ajax帖子(/.auth/me)到主目录/ Auth,设置会话变量并保持它们处于活动状态,直到expiry_on = datetime.now。
谢谢!
答案 0 :(得分:0)
因此,它比预期的要简单得多。不知道为什么没有为此的文档,也许是因为这显然是答案。
这是我从Azure身份验证的重定向中获取.auth / me json的操作。
进行ajax调用以初始调用。
$(document).ready(function () {
$.ajax({
type: "GET",
url: 'https://mcintranet-stage.azurewebsites.net/.auth/me',
success: function (response) {
console.log(response);
Login(response);
},
error: function () {
console.log("Post Failed");
}
});
});
然后将对象-从.auth / me-传递到功能服务器端:
function Login(responseData) {
$.ajax({
type: "POST",
url: `@Url.Action("Login", "Account")`,
data: responseData[0],
success: function (response) {
if (response == "Success") {
console.log("Success Post");
window.location.href = window.location.origin + "/Home/HomeHr";
}
else if (response == "Failed")
console.log("Failed Post");
},
error: function () {
console.log("Post Failed");
}
})
}
服务器端设置授权
[HttpPost]
public string Login(ExternalLogin login)
{
var userToken = login.access_token;
var empId = login.user_id;
var emp = _context.Employees.Where(x => x.Id== empId).FirstOrDefault();
if (UserExists(empId) == true)
{
if (empId != null)
{
HttpContext.Session.SetString("username", empId);
HttpContext.Session.SetString("empId", emp.EmployeeId.ToString());
HttpContext.Session.SetString("user_role", IsInRole(emp.Id));
HttpContext.Session.SetString("name", emp.Fullname);
}
return "Success";
}
return "Failed";
}