2015-02-02 6 views
0

내가 Response.Redirect("tothepageIwant.aspx"); TT하려고 할 때마다 나를 ~/Account/Logon.aspx및 재

에 걸립니다? PrincipalContext.ValidateCredentials을 사용하여 양식 인증을 사용하여 사용자 지정 인증 방법을 사용하고 있습니다.

자격 증명이 유효하면 리디렉션하고 싶습니다. 사용자가 접속할 수 있도록 허용하고있는 페이지로 응답합니다.

대신 성공적으로 로그인하면 이전 Account/Logon.aspx으로 리디렉션됩니다.

제안 사항? 사용자 정의 인증 방법으로 양식 인증을 사용할 때주의해야 할 사항은 무엇입니까?

EDIT (코드를 추가) :

protected void Submit1_Click(object sender, EventArgs e) 
    { 
     var auth = new AuthClass(); 
     var result = auth.ValidateCredentials(UserEmail.Text, UserPass.Text); 
     if (result) 
     { 
      Response.Redirect("~/Members/RollReport.aspx"); 
     } 
     else 
     { 
      Msg.Text = "Not authorized to access this page."; 
     } 
    } 

    public bool ValidateCredentials(string user, string pass) 
    { 
     using (var pc = new PrincipalContext(ContextType.Domain, "Domain.name")) 
     { 
      // validate the credentials 
      try 
      { 
       var isValid = pc.ValidateCredentials(user, pass); 
       if (isValid) 
       { 
        var isAuth = AuthorizeUser(user); 
        return isAuth; 
       } 
       else 
       { 
        return false; 
       } 
      } 
      catch (ActiveDirectoryOperationException) 
      { 
       throw; 
      } 
     } 
    } 

    private bool AuthorizeUser(string user) 
    { 
     var isAuth = false; 
     var authList = (List<string>)HttpContext.Current.Cache["AuthList"]; 
     foreach (var id in authList) 
     { 
      if (id == user) 
      { 
       isAuth = true; 
      } 
     } 
     return isAuth; 
    } 
+0

우리가 잘못 가고있는 곳을보기 위해서 ..'Global.asax' 파일의'OnSessionStart()'이벤트에서 PrincipalContext를 적절히 사용하여 초기화하는 세션 변수를 사용하면 더 쉽게 할 수 있습니다. – MethodMan

+0

인증으로 편집 됨 메소드 코드. – terbubbs

+0

반드시 인증하는 데 문제가있는 것은 아닙니다. 솔직히 말해, 내 사용자 지정 메서드로 양식 인증을 사용하는 것이 맞는지 잘 모르겠습니다. – terbubbs

답변

1
var userName = Request.ServerVariables["LOGON_USER"];//or some other method of capturing the value from the username 
var pc = new PrincipalContext(ContextType.Domain); 
var userFind = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, userName); 
if(userFind != null) 
{ 
    HttpContext.Current.Session["username"] = userFind.DisplayName; 
} 

확인 및 재 지정하려면 ..이의를 Page_Load에 Global.asax

protected void Session_Start(object sender, EventArgs e) 
{ 
    //declare and Initialize your LogIn Session variable 
    HttpContext.Current.Session["username"] = string.Empty; 
} 

내부 세션 변수 안에 값을 저장 위의 코드가 성공하면 로그인 페이지에 값이 할당됩니다.

if(HttpContext.Current.Session["username"] == null) 
    { 
     //Force them to redirect to the login page 
    } 
    else 
    { 
     Response.Redirect("tothepageIwant.aspx"); 
    } 
(210)

if you want to do the same thing inside a using(){} statement

string fullName = null; 
using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) 
{ 
    using (UserPrincipal user = UserPrincipal.FindByIdentity(context,"yourusernamehere")) //User.Identity.Name 
    { 
     if (user != null) 
     { 
      fullName = user.DisplayName; 
     } 
    } 
} 

디버거를 사용하여 모든 사항을 점검 user.Properties 확인

이 아마도 도움이 될 몇 가지 코드를 보여 ...를 Page_Load 이벤트 중 하나 또는 둘 모두의 코드 로직과 관련이있다
+0

나는 이것을 시도했다. 자, 내가하지 않는게있어? 세션 [ "username"]을 초기화하고 Response.Redirect가 나오면 세션이 ""으로 시작됩니다. – terbubbs

+0

'HttpContext.Current.Session vs Session [ "username"]'의 차이점이 있습니다. 또한 사용자 이름을 어떻게 전달하는지에 대한 예를들 수 있습니다. username 또는 domain \ username입니까? – MethodMan

+0

코드의 처음 3 줄은 사용자 이름을 harcodeing하여 독립적으로 테스트 할 수 있습니다.'userFind' 변수가 예상 값을 반환하는지 보는 것은 매우 간단합니다. – MethodMan