2013-04-16 31 views

Trả lời

3

Nếu bạn lưu trữ trên IIS với iisnode https://github.com/auth0/passport-windowsauth hoạt động độc đáo! Hộ chiếu-windowsauth đi kèm với một tích hợp quảng cáo nhưng nếu bạn chỉ muốn tên người dùng để thực hiện logic authorzation riêng của bạn, bạn có thể làm điều đó như thế này

web.config:

<system.webServer> 
    <iisnode promoteServerVars="LOGON_USER" /> 
</system.webServer> 

server.js:

var passport = require('passport'); 
var WindowsStrategy = require('passport-windowsauth'); 

app.use(passport.initialize()); 
app.use(passport.session()); 

passport.serializeUser(function(user, done) { 
    done(null, user); 
}); 

passport.deserializeUser(function(user, done) { 
    done(null, user); 
}); 

passport.use(new WindowsStrategy({ 
    integrated: true 
}, function(profile,done) { 
    var user = { 
     id: profile.id, 
    }; 
    done(null, user); 
})); 

app.all("*", passport.authenticate("WindowsAuthentication"), function (request,response,next){ 
    next(); 
}); 

sau đó bạn có thể truy cập vào userid vào đối tượng yêu cầu trong các tuyến đường khác của bạn:

app.get("/api/testAuthentication", function(request, response){ 
    console.log(request.user.id + " is authenticated"); 
}); 

nếu bạn muốn thực hiện logic uỷ quyền của riêng bạn bằng cách sử dụng id người dùng mà bạn có thể định nghĩa một hàm trung như thế này:

app.get("/api/testAuthorization", hasRole("a role"), function(request, response, next){ 
    console.log(request.user.id " is authenticated and authorized"); 
}); 

nơi hasRole trông như thế này:

function hasRole(role) { 
    return function(request,response,next){ 
     //your own authorzation logic 

     if(role == "a role") 
      next(); 
     else 
      response.status(403).send(); 
    } 
} 
+0

Nhưng khi chúng tôi đặt nút sau IIS, chúng tôi mất rất nhiều lợi ích của nút! – DaNeSh

+0

Tôi có thể nhận được phản hồi '(request.user.id)' từ Url trong trình duyệt. nhưng khi tôi đang cố gắng truy cập cùng một URL, tôi gặp lỗi Lỗi trái phép. Tôi biết bài viết này là cũ nhưng nếu có thể, bạn có thể chia sẻ bất kỳ ví dụ mà bạn đã sử dụng loại điều? –

Các vấn đề liên quan