사용자가 로그인 버튼을 클릭 할 때 로그인 할 수있는 간단한 로그인 페이지를 설정했습니다. 사용자는 로그인시 역할이 할당됩니다. 작동하는지 테스트하기 위해 로그인을 위해 다음 코드를 수행했습니다.
[HttpPost]
[ActionName("Login")]
public ActionResult Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
string userName = model.Username;
string[] userRoles = new string[5];
userRoles[0] = "Administrator";
ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userName));
// userRoles.ToList().ForEach((role) => identity.AddClaim(new Claim(ClaimTypes.Role, role)));
identity.AddClaim(new Claim(ClaimTypes.Role, userRoles[0]));
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
AuthenticationManager.SignIn(identity);
return RedirectToAction("Success");
}
else
{
return View("Login",model);
}
}
그리고 사용자가 로그인 후 실제로 액세스 할 수 있는지 확인하기 위해 MVC 작업에 Authorize 속성을 추가했습니다. 수행 방법은 다음과 같습니다.
[Authorize(Roles="Administrator")]
public ActionResult Register()
{
var model = new UserRegistrationViewModel();
var countries = Connection.ctx.Countries.OrderBy(x => x.CountryName).ToList();
model.Countries = new SelectList(countries, "CountryId", "CountryName");
return View(model);
}
하지만 어떤 이유로 다음과 같이 액세스하려고 할 때 :
mywebsite.com/user/register
나에게 보여줍니다.
HTTP Error 401.0 - Unauthorized
You do not have permission to view this directory or page.
뭐가 될수 있었는지 ?
편집하다:
다음은 사용자가 로그인 한 후의 클레임 및 ID 스냅 샷입니다.
그리고 두 번째 :
쿠키 미들웨어가 있는지 확인할 수 있습니까? 예를 들면
[assembly: OwinStartup(typeof(YourApplicationName.Startup))]
namespace YourApplicationName
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다